我来帮您设计一个Java程序来统计红黄牌数量,并比较哪队更多。

import java.util.*;
public class CardStatistics {
// 队伍类
static class Team {
String name;
int yellowCards = 0;
int redCards = 0;
int totalCards = 0;
public Team(String name) {
this.name = name;
}
void addYellowCard() {
yellowCards++;
totalCards++;
}
void addRedCard() {
redCards++;
totalCards++;
}
int getCardPoints() {
// 红牌算2分,黄牌算1分(用于综合比较)
return redCards * 2 + yellowCards;
}
@Override
public String toString() {
return String.format("%s: 红牌=%d, 黄牌=%d, 总数=%d, 积分=%d",
name, redCards, yellowCards, totalCards, getCardPoints());
}
}
public static void main(String[] args) {
// 模拟数据
Map<String, Team> teams = new HashMap<>();
// 创建队伍
teams.put("巴西", new Team("巴西"));
teams.put("德国", new Team("德国"));
teams.put("阿根廷", new Team("阿根廷"));
teams.put("法国", new Team("法国"));
// 模拟比赛数据(示例假设)
simulateMatches(teams);
// 统计并比较
compareTeams(teams);
}
// 模拟比赛数据
private static void simulateMatches(Map<String, Team> teams) {
// 示例数据:巴西对德国
Team brazil = teams.get("巴西");
Team germany = teams.get("德国");
// 巴西球员获得1黄牌
brazil.addYellowCard();
// 德国球员获得1红牌1黄牌
germany.addRedCard();
germany.addYellowCard();
// 阿根廷对法国
Team argentina = teams.get("阿根廷");
Team france = teams.get("法国");
// 阿根廷获得2黄牌
argentina.addYellowCard();
argentina.addYellowCard();
// 法国获得1黄牌
france.addYellowCard();
// 比赛再次罚牌
brazil.addYellowCard();
germany.addRedCard();
argentina.addYellowCard();
}
// 比较各队红黄牌数量
private static void compareTeams(Map<String, Team> teams) {
System.out.println("=== 各队红黄牌统计 ===");
for (Team team : teams.values()) {
System.out.println(team);
}
System.out.println("\n=== 比较结果 ===");
// 按红牌数量排序(降序)
List<Team> sortedByRedCards = new ArrayList<>(teams.values());
sortedByRedCards.sort((t1, t2) -> Integer.compare(t2.redCards, t1.redCards));
System.out.println("\n红牌最多队:");
int maxRedCards = sortedByRedCards.get(0).redCards;
for (Team team : sortedByRedCards) {
if (team.redCards == maxRedCards) {
System.out.println(" " + team.name + " (红牌: " + team.redCards + ")");
}
}
// 按黄牌数量排序(降序)
List<Team> sortedByYellowCards = new ArrayList<>(teams.values());
sortedByYellowCards.sort((t1, t2) -> Integer.compare(t2.yellowCards, t1.yellowCards));
System.out.println("\n黄牌最多队:");
int maxYellowCards = sortedByYellowCards.get(0).yellowCards;
for (Team team : sortedByYellowCards) {
if (team.yellowCards == maxYellowCards) {
System.out.println(" " + team.name + " (黄牌: " + team.yellowCards + ")");
}
}
// 按总牌数排序(降序)
List<Team> sortedByTotal = new ArrayList<>(teams.values());
sortedByTotal.sort((t1, t2) -> Integer.compare(t2.totalCards, t1.totalCards));
System.out.println("\n总牌数最多队:");
int maxTotalCards = sortedByTotal.get(0).totalCards;
for (Team team : sortedByTotal) {
if (team.totalCards == maxTotalCards) {
System.out.println(" " + team.name + " (总牌数: " + team.totalCards + ")");
}
}
// 按积分排序(红牌2分,黄牌1分)
List<Team> sortedByPoints = new ArrayList<>(teams.values());
sortedByPoints.sort((t1, t2) -> Integer.compare(t2.getCardPoints(), t1.getCardPoints()));
System.out.println("\n积分最多队(红牌=2分,黄牌=1分):");
int maxPoints = sortedByPoints.get(0).getCardPoints();
for (Team team : sortedByPoints) {
if (team.getCardPoints() == maxPoints) {
System.out.println(" " + team.name + " (积分: " + team.getCardPoints() + ")");
}
}
// 总体比较
System.out.println("\n=== 综合评判 ===");
Team worstTeam = sortedByPoints.get(0);
System.out.println("纪律最差队伍:" + worstTeam.name);
// 显示排序结果
System.out.println("\n=== 完整排名(按积分降序)===");
for (int i = 0; i < sortedByPoints.size(); i++) {
Team team = sortedByPoints.get(i);
System.out.println((i+1) + "." + team.name + " - " + team);
}
}
// 添加从文件读取数据的示例方法
public static void loadFromFile(String filename) {
// 这里可以添加从文件读取比赛数据的逻辑
// 格式示例:match_date,team1,team2,card_type,card_color,player_name
}
}
提供一个更实用的版本,支持输入比赛数据:
import java.util.*;
public class CardStatisticsEnhanced {
static class MatchRecord {
String date;
String teamName;
String playerName;
char cardColor; // 'R' - 红牌, 'Y' - 黄牌
public MatchRecord(String date, String teamName, String playerName, char cardColor) {
this.date = date;
this.teamName = teamName;
this.playerName = playerName;
this.cardColor = cardColor;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Map<String, Integer> yellowCards = new HashMap<>();
Map<String, Integer> redCards = new HashMap<>();
List<MatchRecord> allRecords = new ArrayList<>();
System.out.println("=== 红黄牌统计系统 ===");
System.out.println("请选择操作方式:");
System.out.println("1. 输入比赛记录");
System.out.println("2. 使用演示数据");
System.out.print("请输入选择 (1/2): ");
int choice = scanner.nextInt();
scanner.nextLine(); // 消耗换行符
if (choice == 1) {
System.out.println("请输入比赛记录 (格式: 日期 队名 球员名 牌色[Y/R])");
System.out.println("输入 'end' 结束输入");
while (true) {
System.out.print("> ");
String line = scanner.nextLine();
if (line.equalsIgnoreCase("end")) {
break;
}
String[] parts = line.split(" ");
if (parts.length == 4) {
String date = parts[0];
String team = parts[1];
String player = parts[2];
char cardColor = parts[3].toUpperCase().charAt(0);
MatchRecord record = new MatchRecord(date, team, player, cardColor);
allRecords.add(record);
if (cardColor == 'Y') {
yellowCards.merge(team, 1, Integer::sum);
} else if (cardColor == 'R') {
redCards.merge(team, 1, Integer::sum);
}
} else {
System.out.println("格式错误,请按: 日期 队名 球员名 牌色[Y/R]");
}
}
} else {
// 演示数据
simulateDemoData(yellowCards, redCards, allRecords);
}
// 输出统计结果
printStatistics(yellowCards, redCards);
scanner.close();
}
private static void simulateDemoData(Map<String, Integer> yellowCards,
Map<String, Integer> redCards,
List<MatchRecord> records) {
// 模拟一些数据
String[][] demoData = {
{"2024-01-15", "巴西", "卡卡", "R"},
{"2024-01-15", "巴西", "罗纳尔多", "Y"},
{"2024-01-15", "德国", "穆勒", "Y"},
{"2024-01-15", "德国", "克洛泽", "Y"},
{"2024-01-16", "阿根廷", "梅西", "Y"},
{"2024-01-16", "阿根廷", "迪马利亚", "Y"},
{"2024-01-16", "法国", "姆巴佩", "R"},
{"2024-01-16", "法国", "格里兹曼", "Y"},
{"2024-01-17", "西班牙", "因涅斯塔", "Y"},
{"2024-01-17", "西班牙", "哈维", "R"},
{"2024-01-17", "意大利", "皮尔洛", "Y"},
{"2024-01-17", "意大利", "布冯", "R"}
};
for (String[] data : demoData) {
String date = data[0];
String team = data[1];
String player = data[2];
char cardColor = data[3].charAt(0);
MatchRecord record = new MatchRecord(date, team, player, cardColor);
records.add(record);
if (cardColor == 'Y') {
yellowCards.merge(team, 1, Integer::sum);
} else if (cardColor == 'R') {
redCards.merge(team, 1, Integer::sum);
}
}
System.out.println("已加载演示数据 (" + records.size() + " 条记录)");
}
private static void printStatistics(Map<String, Integer> yellowCards,
Map<String, Integer> redCards) {
System.out.println("\n========== 红黄牌统计结果 ==========");
System.out.printf("%-10s %-10s %-10s %-10s %-10s%n", "队名", "红牌", "黄牌", "总牌数", "积分");
System.out.println("-------------------------------------------");
Set<String> allTeams = new HashSet<>();
allTeams.addAll(yellowCards.keySet());
allTeams.addAll(redCards.keySet());
List<Object[]> teamStats = new ArrayList<>();
for (String team : allTeams) {
int red = redCards.getOrDefault(team, 0);
int yellow = yellowCards.getOrDefault(team, 0);
int total = red + yellow;
int points = red * 2 + yellow; // 红牌2分,黄牌1分
teamStats.add(new Object[]{team, red, yellow, total, points});
System.out.printf("%-10s %-10d %-10d %-10d %-10d%n",
team, red, yellow, total, points);
}
// 排序并找出最多牌的队伍
teamStats.sort((a, b) -> Integer.compare((int)b[4], (int)a[4]));
if (!teamStats.isEmpty()) {
Object[] topTeam = teamStats.get(0);
System.out.println("\n===== 排行榜 =====");
System.out.println("红黄牌最多(积分最高)的球队: " + topTeam[0]);
System.out.println("红牌: " + topTeam[1] + ", 黄牌: " + topTeam[2] +
", 总牌数: " + topTeam[3] + ", 积分: " + topTeam[4]);
// 详细排名
System.out.println("\n完整排名:");
int rank = 1;
for (Object[] stats : teamStats) {
System.out.printf("第%d名: %s (红牌%d, 黄牌%d, 积分%d)%n",
rank++, stats[0], stats[1], stats[2], stats[4]);
}
}
}
}
这个程序的功能特点:
- 数据结构:使用HashMap存储各队的红黄牌数据
- 统计功能:统计红牌、黄牌数量,计算总牌数和积分
- 比较功能:找出红牌最多、黄牌最多、总牌数最多的球队
- 排名系统:按积分进行排名(红牌2分,黄牌1分)
- 灵活输入:支持手动输入比赛记录或使用演示数据
- 输出清晰:表格形式展示统计结果
运行示例:
========== 红黄牌统计结果 ==========
队名 红牌 黄牌 总牌数 积分
-------------------------------------------
西班牙 1 1 2 3
法国 1 1 2 3
德国 0 2 2 2
意大利 1 1 2 3
巴西 1 1 2 3
阿根廷 0 2 2 2
===== 排行榜 =====
红黄牌最多(积分最高)的球队: 西班牙
红牌: 1, 黄牌: 1, 总牌数: 2, 积分: 3
您可以根据实际需求调整统计标准和比较规则。