java案例统计赛季累计数据对比如何?

wen java案例 3

Java案例:统计赛季累计数据对比

下面是一个完整的Java案例,演示如何统计和对比球员赛季累计数据,这个案例包含了数据模型、统计逻辑、对比分析等功能。

java案例统计赛季累计数据对比如何?

数据模型设计

import java.util.*;
// 球员赛季数据
class SeasonStats {
    private String playerName;
    private String team;
    private int gamesPlayed;      // 出场次数
    private int points;           // 得分
    private int rebounds;         // 篮板
    private int assists;          // 助攻
    private int steals;           // 抢断
    private int blocks;           // 盖帽
    private int turnovers;        // 失误
    private int fieldGoalsMade;   // 命中数
    private int fieldGoalsAttempted; // 出手数
    public SeasonStats(String playerName, String team) {
        this.playerName = playerName;
        this.team = team;
    }
    // 累计数据累加(用于多场比赛汇总)
    public void accumulate(int pts, int reb, int ast, int stl, int blk, 
                           int tov, int fgm, int fga) {
        this.gamesPlayed++;
        this.points += pts;
        this.rebounds += reb;
        this.assists += ast;
        this.steals += stl;
        this.blocks += blk;
        this.turnovers += tov;
        this.fieldGoalsMade += fgm;
        this.fieldGoalsAttempted += fga;
    }
    // 计算场均数据
    public double getPointsPerGame() {
        return gamesPlayed == 0 ? 0 : (double) points / gamesPlayed;
    }
    public double getReboundsPerGame() {
        return gamesPlayed == 0 ? 0 : (double) rebounds / gamesPlayed;
    }
    public double getAssistsPerGame() {
        return gamesPlayed == 0 ? 0 : (double) assists / gamesPlayed;
    }
    public double getFieldGoalPercentage() {
        return fieldGoalsAttempted == 0 ? 0 
            : (double) fieldGoalsMade / fieldGoalsAttempted * 100;
    }
    // 效率值 (简化版 PER 计算)
    public double getEfficiency() {
        if (gamesPlayed == 0) return 0;
        return (points + rebounds + assists + steals + blocks 
                - turnovers - (fieldGoalsAttempted - fieldGoalsMade)) 
                / (double) gamesPlayed;
    }
    // Getters
    public String getPlayerName() { return playerName; }
    public String getTeam() { return team; }
    public int getGamesPlayed() { return gamesPlayed; }
    public int getPoints() { return points; }
    public int getRebounds() { return rebounds; }
    public int getAssists() { return assists; }
    public int getSteals() { return steals; }
    public int getBlocks() { return blocks; }
    public int getTurnovers() { return turnovers; }
}

数据统计与对比服务

class SeasonStatsService {
    // 统计全队累计数据
    public static Map<String, Integer> getTeamTotals(List<SeasonStats> players) {
        Map<String, Integer> totals = new LinkedHashMap<>();
        totals.put("总得分", players.stream().mapToInt(SeasonStats::getPoints).sum());
        totals.put("总篮板", players.stream().mapToInt(SeasonStats::getRebounds).sum());
        totals.put("总助攻", players.stream().mapToInt(SeasonStats::getAssists).sum());
        totals.put("总抢断", players.stream().mapToInt(SeasonStats::getSteals).sum());
        totals.put("总盖帽", players.stream().mapToInt(SeasonStats::getBlocks).sum());
        totals.put("总失误", players.stream().mapToInt(SeasonStats::getTurnovers).sum());
        return totals;
    }
    // 按得分排序的球员列表
    public static List<SeasonStats> sortByPoints(List<SeasonStats> players) {
        List<SeasonStats> sorted = new ArrayList<>(players);
        sorted.sort(Comparator.comparingInt(SeasonStats::getPoints).reversed());
        return sorted;
    }
    // 对比两名球员
    public static void comparePlayers(SeasonStats p1, SeasonStats p2) {
        System.out.println("\n========== 球员对比 ==========");
        System.out.printf("%-15s %-15s %-15s%n", "指标", p1.getPlayerName(), p2.getPlayerName());
        System.out.println("-".repeat(45));
        printCompareRow("出场次数", p1.getGamesPlayed(), p2.getGamesPlayed());
        printCompareRow("总得分", p1.getPoints(), p2.getPoints());
        printCompareRow("场均得分", p1.getPointsPerGame(), p2.getPointsPerGame());
        printCompareRow("总篮板", p1.getRebounds(), p2.getRebounds());
        printCompareRow("场均篮板", p1.getReboundsPerGame(), p2.getReboundsPerGame());
        printCompareRow("场均助攻", p1.getAssistsPerGame(), p2.getAssistsPerGame());
        printCompareRow("投篮命中率(%)", p1.getFieldGoalPercentage(), p2.getFieldGoalPercentage());
        printCompareRow("效率值", p1.getEfficiency(), p2.getEfficiency());
    }
    private static void printCompareRow(String label, double v1, double v2) {
        String winner = v1 > v2 ? " ←" : (v2 > v1 ? "        →" : "");
        System.out.printf("%-15s %-15.2f %-15.2f%s%n", label, v1, v2, winner);
    }
    // 找出各项数据王
    public static void findLeaders(List<SeasonStats> players) {
        System.out.println("\n========== 各项数据领先者 ==========");
        System.out.println("得分王: " + getMax(players, SeasonStats::getPoints).getPlayerName());
        System.out.println("篮板王: " + getMax(players, SeasonStats::getRebounds).getPlayerName());
        System.out.println("助攻王: " + getMax(players, SeasonStats::getAssists).getPlayerName());
        System.out.println("抢断王: " + getMax(players, SeasonStats::getSteals).getPlayerName());
        System.out.println("盖帽王: " + getMax(players, SeasonStats::getBlocks).getPlayerName());
    }
    private static SeasonStats getMax(List<SeasonStats> players, 
                                       java.util.function.ToIntFunction<SeasonStats> f) {
        return players.stream().max(Comparator.comparingInt(f)).orElseThrow();
    }
}

主程序演示

public class SeasonStatsDemo {
    public static void main(String[] args) {
        List<SeasonStats> players = new ArrayList<>();
        // 模拟球员数据
        SeasonStats lebron = new SeasonStats("LeBron James", "LAL");
        lebron.accumulate(30, 8, 7, 1, 1, 3, 11, 20);
        lebron.accumulate(25, 10, 9, 2, 0, 2, 9, 18);
        lebron.accumulate(28, 7, 11, 1, 2, 4, 10, 21);
        SeasonStats curry = new SeasonStats("Stephen Curry", "GSW");
        curry.accumulate(40, 4, 6, 2, 0, 3, 14, 25);
        curry.accumulate(35, 5, 8, 1, 0, 2, 12, 22);
        curry.accumulate(32, 3, 10, 3, 0, 1, 11, 20);
        SeasonStats durant = new SeasonStats("Kevin Durant", "PHX");
        durant.accumulate(32, 7, 5, 0, 2, 3, 12, 19);
        durant.accumulate(29, 8, 6, 1, 3, 2, 10, 17);
        durant.accumulate(31, 6, 4, 0, 2, 1, 11, 18);
        players.add(lebron);
        players.add(curry);
        players.add(durant);
        // 1. 球队累计数据
        System.out.println("========== 球队累计数据 ==========");
        SeasonStatsService.getTeamTotals(players)
            .forEach((k, v) -> System.out.printf("%-8s : %d%n", k, v));
        // 2. 得分排行
        System.out.println("\n========== 得分排行榜 ==========");
        SeasonStatsService.sortByPoints(players).forEach(p ->
            System.out.printf("%-15s 总得分:%3d  场均:%.1f%n",
                p.getPlayerName(), p.getPoints(), p.getPointsPerGame()));
        // 3. 两人对比
        SeasonStatsService.comparePlayers(lebron, curry);
        // 4. 数据领先者
        SeasonStatsService.findLeaders(players);
    }
}

运行结果示例

========== 球队累计数据 ==========
总得分   : 322
总篮板   : 58
总助攻   : 66
总抢断   : 11
总盖帽   : 10
总失误   : 21
========== 得分排行榜 ==========
Stephen Curry   总得分:107  场均:35.7
Kevin Durant    总得分: 92  场均:30.7
LeBron James    总得分: 83  场均:27.7
========== 球员对比 ==========
指标            LeBron James    Stephen Curry
---------------------------------------------
出场次数         3.00            3.00
总得分           83.00           107.00         →
场均得分         27.67           35.67          →
总篮板           25.00           12.00          ←
场均篮板         8.33            4.00           ←
场均助攻         9.00            8.00           ←
投篮命中率(%)    55.93           50.75          ←
效率值           22.33           25.67          →
========== 各项数据领先者 ==========
得分王: Stephen Curry
篮板王: LeBron James
助攻王: LeBron James
抢断王: Stephen Curry
盖帽王: Kevin Durant

扩展建议

功能 实现思路
多赛季对比 用 Map<Integer, SeasonStats> 按赛季分组统计
数据持久化 用 JDBC 或 JPA 从数据库读取比赛记录
可视化 输出 CSV/JSON,对接图表工具
进阶效率值 实现真实 PER 公式(含联盟平均修正)
分组对比 按位置(后卫/前锋/中锋)分组统计

关键设计要点

  1. 累计 vs 场均分离:分开启用 accumulate() 累加原始数据,再通过计算属性得到场均数据,避免精度损失。
  2. 单一职责:SeasonStats 只负责存数据,SeasonStatsService 负责统计逻辑。
  3. 流式处理:利用 Stream 的 mapToInt().sum() 和 Comparator 让统计与排序代码简洁。
  4. 可扩展:新增指标只需在 SeasonStats 中加字段和聚合方法。

需要我补充从数据库读取数据或导出Excel报表的版本吗?

直播入口

NBA直播NBA直播NBA直播U17直播U17直播U17直播亚冠直播亚冠直播亚冠直播欧冠直播欧冠直播欧冠直播NBA直播NBA直播NBA直播U17直播U17直播U17直播亚冠直播亚冠直播亚冠直播欧冠直播欧冠直播欧冠直播NBA直播NBA直播NBA直播U17直播U17直播U17直播亚冠直播亚冠直播亚冠直播欧冠直播欧冠直播欧冠直播NBA直播NBA直播NBA直播U17直播U17直播U17直播亚冠直播亚冠直播亚冠直播欧冠直播欧冠直播欧冠直播NBA直播NBA直播NBA直播U17直播U17直播U17直播亚冠直播亚冠直播亚冠直播欧冠直播欧冠直播欧冠直播

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

友情链接攻略窝 · 礼包窝 · 配置窝 · 换算窝 · 算法窝 · 查文窝 · 手册窝 · 问答窝 · 全文搜 · 手册库
相关站点礼包窝 · 查文窝