java案例怎么看两队拦截抢断数据?

wen java案例 1

Java 案例:查看两队拦截抢断数据

下面给你一个完整的 Java 案例,模拟"查看两支球队的拦截、抢断数据"功能,包含数据模型、统计逻辑、查询和展示。

java案例怎么看两队拦截抢断数据?

需求场景

  • 有两支球队(如 A 队、B 队)
  • 每队有若干球员
  • 每个球员有:拦截次数(interceptions)、抢断次数(tackles)
  • 需求:分别查看每队的拦截、抢断总数、平均值,并对比两队

完整代码

球员实体类

public class Player {
    private String name;
    private String team;
    private int interceptions; // 拦截
    private int tackles;       // 抢断
    public Player(String name, String team, int interceptions, int tackles) {
        this.name = name;
        this.team = team;
        this.interceptions = interceptions;
        this.tackles = tackles;
    }
    public String getName() { return name; }
    public String getTeam() { return team; }
    public int getInterceptions() { return interceptions; }
    public int getTackles() { return tackles; }
    @Override
    public String toString() {
        return String.format("%-6s 拦截:%d  抢断:%d", name, interceptions, tackles);
    }
}

统计结果类

public class TeamStats {
    private String teamName;
    private int totalInterceptions;
    private int totalTackles;
    private double avgInterceptions;
    private double avgTackles;
    public TeamStats(String teamName, int totalInterceptions, int totalTackles,
                     double avgInterceptions, double avgTackles) {
        this.teamName = teamName;
        this.totalInterceptions = totalInterceptions;
        this.totalTackles = totalTackles;
        this.avgInterceptions = avgInterceptions;
        this.avgTackles = avgTackles;
    }
    public int getTotalInterceptions() { return totalInterceptions; }
    public int getTotalTackles() { return totalTackles; }
    @Override
    public String toString() {
        return String.format("球队: %s | 总拦截: %d | 总抢断: %d | 场均拦截: %.2f | 场均抢断: %.2f",
                teamName, totalInterceptions, totalTackles,
                avgInterceptions, avgTackles);
    }
}

主程序:统计与对比

import java.util.*;
import java.util.stream.Collectors;
public class MatchStatsDemo {
    public static void main(String[] args) {
        // 1. 初始化球员数据
        List<Player> players = Arrays.asList(
                new Player("张三", "A队", 3, 5),
                new Player("李四", "A队", 2, 4),
                new Player("王五", "A队", 4, 2),
                new Player("赵六", "A队", 1, 3),
                new Player("孙七", "B队", 5, 6),
                new Player("周八", "B队", 3, 4),
                new Player("吴九", "B队", 2, 5),
                new Player("郑十", "B队", 4, 3)
        );
        // 2. 按球队分组统计
        Map<String, List<Player>> grouped = players.stream()
                .collect(Collectors.groupingBy(Player::getTeam));
        List<TeamStats> statsList = new ArrayList<>();
        for (Map.Entry<String, List<Player>> entry : grouped.entrySet()) {
            String team = entry.getKey();
            List<Player> list = entry.getValue();
            int totalInter = list.stream().mapToInt(Player::getInterceptions).sum();
            int totalTackle = list.stream().mapToInt(Player::getTackles).sum();
            double avgInter = list.stream().mapToInt(Player::getInterceptions).average().orElse(0);
            double avgTackle = list.stream().mapToInt(Player::getTackles).average().orElse(0);
            statsList.add(new TeamStats(team, totalInter, totalTackle, avgInter, avgTackle));
        }
        // 3. 打印每队详细数据
        System.out.println("====== 各队球员明细 ======");
        for (Map.Entry<String, List<Player>> entry : grouped.entrySet()) {
            System.out.println("【" + entry.getKey() + "】");
            entry.getValue().forEach(p -> System.out.println("  " + p));
            System.out.println();
        }
        // 4. 打印汇总统计
        System.out.println("====== 球队汇总统计 ======");
        statsList.forEach(System.out::println);
        // 5. 两队对比
        System.out.println("\n====== 两队对比 ======");
        if (statsList.size() >= 2) {
            TeamStats t1 = statsList.get(0);
            TeamStats t2 = statsList.get(1);
            System.out.println("拦截更多: " +
                    (t1.getTotalInterceptions() > t2.getTotalInterceptions()
                            ? t1 : t2).toString());
            System.out.println("抢断更多: " +
                    (t1.getTotalTackles() > t2.getTotalTackles()
                            ? t1 : t2).toString());
        }
    }
}

运行结果示例

====== 各队球员明细 ======
【A队】
  张三   拦截:3  抢断:5
  李四   拦截:2  抢断:4
  王五   拦截:4  抢断:2
  赵六   拦截:1  抢断:3
【B队】
  孙七   拦截:5  抢断:6
  周八   拦截:3  抢断:4
  吴九   拦截:2  抢断:5
  郑十   拦截:4  抢断:3
====== 球队汇总统计 ======
球队: A队 | 总拦截: 10 | 总抢断: 14 | 场均拦截: 2.50 | 场均抢断: 3.50
球队: B队 | 总拦截: 14 | 总抢断: 18 | 场均拦截: 3.50 | 场均抢断: 4.50
====== 两队对比 ======
拦截更多: 球队: B队 | 总拦截: 14 | 总抢断: 18 | 场均拦截: 3.50 | 场均抢断: 4.50
抢断更多: 球队: B队 | 总拦截: 14 | 总抢断: 18 | 场均拦截: 3.50 | 场均抢断: 4.50

关键点解析

步骤 用到的技术 说明
数据建模 实体类 Player 表示球员,属性包含拦截/抢断
分组 Collectors.groupingBy 按球队分组
求和 mapToInt().sum() 求每队拦截/抢断总数
平均值 mapToInt().average() 求场均数据
对比 简单比较 找出数据更高的一方

扩展建议

  1. 对接数据库/API:把球员数据从 MySQL 或 REST 接口读取
    List<Player> players = playerDao.findByMatchId(matchId);
  2. 增加字段:解围、犯规、黄牌等,用同样的方式统计
  3. 多场比赛:用 Map<String, List<Player>> 按比赛再分组
  4. 可视化:把统计结果输出为 JSON,交给前端图表(ECharts)展示

如果你结项目数据是从 数据库CSVJSON 来的,或者需要按单场比赛多场比赛区分统计,告诉我具体来源,我可以给你更贴合的代码。

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