java案例统计红黄牌数量哪队更多?

wen java案例 3

Java案例:统计红黄牌数量,判断哪队更多

下面给你一个完整的Java案例,模拟一场足球比赛中裁判出示红黄牌,统计两队各自的红黄牌数量,并判断哪队更多。

java案例统计红黄牌数量哪队更多?

需求分析

  • 每张牌包含:队伍、球员、牌类型(黄牌/红牌)
  • 统计每队的黄牌数、红牌数
  • 比较两队"红黄牌总数",输出哪队更多
  • 如果数量相同,输出"持平"

完整代码

import java.util.*;
// 牌类型枚举
enum CardType {
    YELLOW("黄牌"), RED("红牌");
    private final String name;
    CardType(String name) { this.name = name; }
    public String getName() { return name; }
}
// 罚牌记录
class Card {
    private String team;      // 队伍
    private String player;    // 球员
    private CardType type;    // 牌类型
    public Card(String team, String player, CardType type) {
        this.team = team;
        this.player = player;
        this.type = type;
    }
    public String getTeam() { return team; }
    public String getPlayer() { return player; }
    public CardType getType() { return type; }
    @Override
    public String toString() {
        return String.format("%s队 - %s : %s", team, player, type.getName());
    }
}
// 队伍统计结果
class TeamStat {
    String team;
    int yellow = 0;
    int red = 0;
    public TeamStat(String team) { this.team = team; }
    public int total() { return yellow + red; }
    @Override
    public String toString() {
        return String.format("%s队:黄牌 %d 张,红牌 %d 张,合计 %d 张",
                team, yellow, red, total());
    }
}
public class CardStatistics {
    public static void main(String[] args) {
        // 1. 模拟一张比赛中的罚牌数据
        List<Card> cards = Arrays.asList(
            new Card("A", "张三", CardType.YELLOW),
            new Card("B", "李四", CardType.YELLOW),
            new Card("A", "王五", CardType.RED),
            new Card("A", "赵六", CardType.YELLOW),
            new Card("B", "孙七", CardType.RED),
            new Card("B", "周八", CardType.YELLOW),
            new Card("A", "吴九", CardType.YELLOW),
            new Card("B", "郑十", CardType.YELLOW)
        );
        // 2. 统计
        Map<String, TeamStat> statMap = new LinkedHashMap<>();
        for (Card c : cards) {
            TeamStat stat = statMap.computeIfAbsent(c.getTeam(), TeamStat::new);
            if (c.getType() == CardType.YELLOW) {
                stat.yellow++;
            } else {
                stat.red++;
            }
        }
        // 3. 打印明细
        System.out.println("===== 罚牌明细 =====");
        cards.forEach(System.out::println);
        System.out.println("\n===== 各队统计 =====");
        statMap.values().forEach(System.out::println);
        // 4. 比较哪队更多
        System.out.println("\n===== 结果 =====");
        List<TeamStat> stats = new ArrayList<>(statMap.values());
        if (stats.size() < 2) {
            System.out.println("参赛队伍不足,无法比较。");
            return;
        }
        TeamStat t1 = stats.get(0);
        TeamStat t2 = stats.get(1);
        if (t1.total() > t2.total()) {
            System.out.printf("%s队红黄牌更多(%d : %d)%n", t1.team, t1.total(), t2.total());
        } else if (t1.total() < t2.total()) {
            System.out.printf("%s队红黄牌更多(%d : %d)%n", t2.team, t2.total(), t1.total());
        } else {
            System.out.printf("两队红黄牌数量持平(各 %d 张)%n", t1.total());
        }
    }
}

运行结果

===== 罚牌明细 =====
A队 - 张三 : 黄牌
B队 - 李四 : 黄牌
A队 - 王五 : 红牌
A队 - 赵六 : 黄牌
B队 - 孙七 : 红牌
B队 - 周八 : 黄牌
A队 - 吴九 : 黄牌
B队 - 郑十 : 黄牌
===== 各队统计 =====
A队:黄牌 3 张,红牌 1 张,合计 4 张
B队:黄牌 3 张,红牌 1 张,合计 4 张
===== 结果 =====
两队红黄牌数量持平(各 4 张)

几种常见的"哪队更多"比较方式

需求不同,"更多"的定义也不同,你可以按需替换最后的比较逻辑:

比较方式 代码 说明
只看黄牌 t1.yellow - t2.yellow 比黄牌数
只看红牌 t1.red - t2.red 比红牌数
红黄牌总数 t1.total() - t2.total() 上面案例的写法
加权(红牌=2) t1.yellow + t1.red*2 红牌更严重,给更高权重

加权比较示例:

int score1 = t1.yellow + t1.red * 2;
int score2 = t2.yellow + t2.red * 2;
System.out.println(score1 > score2 ? t1.team + "队罚牌更严重"
                 : score2 > score1 ? t2.team + "队罚牌更严重"
                 : "两队罚牌严重程度相同");

扩展思路

  1. 从文件/数据库读取罚牌数据:把 List<Card> 换成读取 cards.txt 或 JDBC 查询结果。
  2. 支持多队比较:用 TreeMap 排序,直接找出罚牌最多的队伍。
  3. 按球员统计:把 Map<String, TeamStat> 换成按球员聚合,可以找出谁吃牌最多。
  4. 使用 Stream 简化:
Map<String, Long> totalByTeam = cards.stream()
    .collect(Collectors.groupingBy(Card::getTeam, Collectors.counting()));

如果你有具体的输入格式(比如从控制台输入、从文件读取),告诉我,我可以帮你改成对应的版本。

抱歉,评论功能暂时关闭!