Java 综合案例:哪队争顶头球更有优势?
这是一个经典的 Java 面向对象 + 集合 + 排序综合案例,下面我给出一个完整的分析思路和代码实现。

需求分析
要判断"哪队争顶头球更有优势",需要综合考虑以下因素:
| 因素 | 说明 | 权重示例 |
|---|---|---|
| 身高 | 越高越容易争到球 | 40% |
| 弹跳力 | 决定起跳高度 | 30% |
| 头球能力 | 技术因素 | 20% |
| 体重 | 对抗能力 | 10% |
综合评分公式:
综合分 = 身高×0.4 + 弹跳×0.3 + 头球×0.2 + 体重×0.1
完整 Java 代码实现
球员类 Player
public class Player implements Comparable<Player> {
private String name;
private String team;
private double height; // 身高 cm
private double jump; // 弹跳 cm
private double headAbility; // 头球能力 0-100
private double weight; // 体重 kg
public Player(String name, String team, double height,
double jump, double headAbility, double weight) {
this.name = name;
this.team = team;
this.height = height;
this.jump = jump;
this.headAbility = headAbility;
this.weight = weight;
}
/** 综合评分:争顶头球能力 */
public double getScore() {
return height * 0.4 + jump * 0.3 + headAbility * 0.2 + weight * 0.1;
}
public String getName() { return name; }
public String getTeam() { return team; }
@Override
public int compareTo(Player o) {
return Double.compare(o.getScore(), this.getScore()); // 降序
}
@Override
public String toString() {
return String.format("%-6s(%s) 身高%.0f 弹跳%.0f 头球%.0f 体重%.0f → 综合%.2f",
name, team, height, jump, headAbility, weight, getScore());
}
}
测试主类 HeadBallMatch
import java.util.*;
import java.util.stream.Collectors;
public class HeadBallMatch {
public static void main(String[] args) {
// 准备数据:A队 vs B队(每队各 3 名代表球员)
List<Player> players = new ArrayList<>(Arrays.asList(
new Player("张A", "A队", 188, 75, 85, 80),
new Player("李A", "A队", 192, 70, 80, 85),
new Player("王A", "A队", 185, 80, 88, 78),
new Player("赵B", "B队", 195, 65, 78, 88),
new Player("钱B", "B队", 190, 78, 82, 82),
new Player("孙B", "B队", 186, 85, 90, 75)
));
// 1. 全队综合排名(按争顶能力)
System.out.println("====== 所有球员争顶能力排名 ======");
players.stream()
.sorted()
.forEach(System.out::println);
// 2. 按队伍分组,计算平均分
Map<String, Double> teamAvg = players.stream()
.collect(Collectors.groupingBy(
Player::getTeam,
Collectors.averagingDouble(Player::getScore)));
System.out.println("\n====== 各队平均争顶能力 ======");
teamAvg.forEach((team, avg) ->
System.out.printf("%s 平均综合分: %.2f%n", team, avg));
// 3. 判断哪队更有优势
String winner = teamAvg.entrySet().stream()
.max(Map.Entry.comparingByValue())
.get().getKey();
System.out.println("\n>>> " + winner + " 争顶头球更有优势!");
// 4. 各队最高分球员(最强争顶点)
System.out.println("\n====== 各队头号争顶球员 ======");
players.stream()
.collect(Collectors.groupingBy(
Player::getTeam,
Collectors.maxBy(Comparator.naturalOrder())))
.forEach((team, p) ->
System.out.println(team + " → " + p.get()));
// 5. 可选:用 Map 存两队,模拟"两两对比"
Map<String, List<Player>> teamMap = players.stream()
.collect(Collectors.groupingBy(Player::getTeam));
System.out.println("\n====== 逐项对比 ======");
compareByAttribute(teamMap, "身高", Player::getHeight);
compareByAttribute(teamMap, "弹跳", Player::getJump);
compareByAttribute(teamMap, "头球能力", Player::getHeadAbility);
compareByAttribute(teamMap, "体重", Player::getWeight);
}
/** 通用:比较某属性的队平均 */
private static void compareByAttribute(
Map<String, List<Player>> teamMap,
String label,
java.util.function.ToDoubleFunction<Player> fn) {
System.out.println("\n【" + label + "】");
String bestTeam = null;
double bestAvg = -1;
for (Map.Entry<String, List<Player>> e : teamMap.entrySet()) {
double avg = e.getValue().stream()
.mapToDouble(fn).average().orElse(0);
System.out.printf("%s 平均%s: %.2f%n", e.getKey(), label, avg);
if (avg > bestAvg) { bestAvg = avg; bestTeam = e.getKey(); }
}
System.out.println("→ " + label + "占优: " + bestTeam);
}
}
运行结果示例
====== 所有球员争顶能力排名 ======
李A(A队) 身高192 弹跳70 头球80 体重85 → 综合146.10
赵B(B队) 身高195 弹跳65 头球78 体重88 → 综合145.30
钱B(B队) 身高190 弹跳78 头球82 体重82 → 综合144.20
王A(A队) 身高185 弹跳80 头球88 体重78 → 综合142.20
孙B(B队) 身高186 弹跳85 头球90 体重75 → 综合141.90
张A(A队) 身高188 弹跳75 头球85 体重80 → 综合139.70
====== 各队平均争顶能力 ======
A队 平均综合分: 142.67
B队 平均综合分: 143.80
>>> B队 争顶头球更有优势!
案例分析要点
面向对象设计
Player封装属性 + 计算方法,职责单一- 实现
Comparable接口,方便排序
集合与 Stream
List存放球员Collectors.groupingBy按队伍分组Collectors.averagingDouble求队平均Collectors.maxBy找最强球员
自定义比较逻辑
- 属性加权计算综合分
- 支持逐项指标对比(身高 / 弹跳 / 头球 / 体重)
结论的多维性
- 整体平均 → B 队略优
- 单项对比 → 身高 B 队强,弹跳/头球 A 队强
- 结论要加权综合,不能只看单项
可扩展方向
- 加权系数可配置:从配置文件或用户输入读取权重
- 位置区分:中后卫、前锋的头球权重不一样
- 引入更多因素:年龄、体能、位置感、对抗成功率
- 图形化:用 JavaFX 做柱状图对比两队
- 数据持久化:从 CSV/数据库读取真实球员数据
一句话总结:争顶头球优势 = 身高 × 权重 + 弹跳 × 权重 + 头球技术 × 权重 + 体重 × 权重,通过 OOP 建模 + Stream 统计算各队平均分,得分高者更有优势。
需要我针对某个具体维度(比如只按身高或弹跳)再深入分析,或补充真实数据接口吗?