本文目录导读:

我来帮你创建一个统计门将扑救次数的Java案例,这里我设计一个完整的足球比赛系统来展示门将的"忙碌程度":
基础类设计
import java.util.*;
import java.util.stream.Collectors;
// 门将类
class Goalkeeper {
private String name;
private String team;
private int saves; // 扑救次数
private int goalsConceded; // 失球数
private int clearances; // 解围次数
private int catches; // 接住球次数
public Goalkeeper(String name, String team) {
this.name = name;
this.team = team;
this.saves = 0;
this.goalsConceded = 0;
this.clearances = 0;
this.catches = 0;
}
// 记录一次扑救
public void recordSave() {
this.saves++;
}
// 记录失球
public void recordGoalConceded() {
this.goalsConceded++;
}
// 记录解围
public void recordClearance() {
this.clearances++;
}
// 记录接球
public void recordCatch() {
this.catches++;
}
// 计算忙碌指数(综合评分)
public double getBusynessScore() {
// 权重:扑救最重要,其次是解围和接球
return saves * 1.0 + clearances * 0.6 + catches * 0.4;
}
// Getters
public String getName() { return name; }
public String getTeam() { return team; }
public int getSaves() { return saves; }
public int getGoalsConceded() { return goalsConceded; }
public int getClearances() { return clearances; }
public int getCatches() { return catches; }
@Override
public String toString() {
return String.format("%s(%s) - 扑救:%d, 失球:%d, 解围:%d, 接球:%d, 忙碌指数:%.1f",
name, team, saves, goalsConceded, clearances, catches, getBusynessScore());
}
}
// 比赛模拟类
class MatchSimulator {
private List<Goalkeeper> goalkeepers;
private Random random;
public MatchSimulator() {
this.goalkeepers = new ArrayList<>();
this.random = new Random();
}
// 添加门将
public void addGoalkeeper(Goalkeeper gk) {
goalkeepers.add(gk);
}
// 模拟一场比赛
public void simulateMatch(int minutes) {
for (Goalkeeper gk : goalkeepers) {
simulateGoalkeeperActivity(gk, minutes);
}
}
// 模拟单个门将的活动
private void simulateGoalkeeperActivity(Goalkeeper gk, int minutes) {
// 根据防守压力随机生成活动
int attackPressure = random.nextInt(100); // 防守压力指数
// 防守压力越大,扑救越多
int saveChance = 10 + attackPressure / 10;
int clearanceChance = 5 + attackPressure / 15;
int catchChance = 8 + attackPressure / 12;
int goalChance = 2 + attackPressure / 30;
// 按分钟模拟
for (int minute = 0; minute < minutes; minute++) {
if (random.nextInt(100) < saveChance) {
gk.recordSave();
}
if (random.nextInt(100) < clearanceChance) {
gk.recordClearance();
}
if (random.nextInt(100) < catchChance) {
gk.recordCatch();
}
if (random.nextInt(100) < goalChance) {
gk.recordGoalConceded();
}
}
}
// 获取最忙的门将
public Goalkeeper getBusiestGoalkeeper() {
return goalkeepers.stream()
.max(Comparator.comparingDouble(Goalkeeper::getBusynessScore))
.orElse(null);
}
// 获取统计报告
public String getStatisticsReport() {
StringBuilder report = new StringBuilder();
report.append("========== 门将忙碌度统计报告 ==========\n");
report.append(String.format("%-20s %-10s %-8s %-8s %-8s %-8s %-10s\n",
"门将", "球队", "扑救", "失球", "解围", "接球", "忙碌指数"));
report.append("-".repeat(80)).append("\n");
// 按忙碌指数排序
List<Goalkeeper> sorted = goalkeepers.stream()
.sorted(Comparator.comparingDouble(Goalkeeper::getBusynessScore).reversed())
.collect(Collectors.toList());
for (Goalkeeper gk : sorted) {
report.append(String.format("%-20s %-10s %-8d %-8d %-8d %-8d %-10.1f\n",
gk.getName(), gk.getTeam(), gk.getSaves(), gk.getGoalsConceded(),
gk.getClearances(), gk.getCatches(), gk.getBusynessScore()));
}
report.append("-".repeat(80)).append("\n");
Goalkeeper busiest = getBusiestGoalkeeper();
if (busiest != null) {
report.append(String.format("\n🏆 最忙门将: %s (球队: %s)\n",
busiest.getName(), busiest.getTeam()));
report.append(String.format(" 扑救次数: %d, 忙碌指数: %.1f\n",
busiest.getSaves(), busiest.getBusynessScore()));
}
return report.toString();
}
// 根据扑救次数排名
public List<Goalkeeper> getTopBySaves(int topN) {
return goalkeepers.stream()
.sorted(Comparator.comparingInt(Goalkeeper::getSaves).reversed())
.limit(topN)
.collect(Collectors.toList());
}
}
主程序和运行示例
public class GoalkeeperBusynessDemo {
public static void main(String[] args) {
// 创建比赛模拟器
MatchSimulator simulator = new MatchSimulator();
// 创建并添加门将
Goalkeeper gk1 = new Goalkeeper("诺伊尔", "拜仁慕尼黑");
Goalkeeper gk2 = new Goalkeeper("特尔施特根", "巴塞罗那");
Goalkeeper gk3 = new Goalkeeper("多纳鲁马", "巴黎圣日耳曼");
Goalkeeper gk4 = new Goalkeeper("库尔图瓦", "皇家马德里");
Goalkeeper gk5 = new Goalkeeper("阿利松", "利物浦");
Goalkeeper gk6 = new Goalkeeper("埃德森", "曼城");
simulator.addGoalkeeper(gk1);
simulator.addGoalkeeper(gk2);
simulator.addGoalkeeper(gk3);
simulator.addGoalkeeper(gk4);
simulator.addGoalkeeper(gk5);
simulator.addGoalkeeper(gk6);
System.out.println("⚽ 开始模拟整个赛季的比赛...\n");
// 模拟一个赛季(假设38场比赛,每场90分钟)
int totalMatches = 38;
int matchMinutes = 90;
for (int match = 1; match <= totalMatches; match++) {
simulator.simulateMatch(matchMinutes);
// 每10场比赛输出一次进度
if (match % 10 == 0) {
System.out.printf("已完成 %d 场比赛...\n", match);
}
}
System.out.println("\n赛季结束!生成统计报告:\n");
// 输出完整统计报告
String report = simulator.getStatisticsReport();
System.out.println(report);
// 额外分析:扑救次数最多的门将
System.out.println("\n🔍 扑救次数TOP 3门将:");
List<Goalkeeper> topSaves = simulator.getTopBySaves(3);
for (int i = 0; i < topSaves.size(); i++) {
Goalkeeper gk = topSaves.get(i);
System.out.printf("第%d名: %s - %d次扑救\n",
i + 1, gk.getName(), gk.getSaves());
}
// 对比分析
System.out.println("\n📊 对比分析:");
Goalkeeper busiest = simulator.getBusiestGoalkeeper();
if (busiest != null) {
System.out.printf("最忙门将: %s (指数: %.1f)\n",
busiest.getName(), busiest.getBusynessScore());
// 找到扑救次数最多的门将
Goalkeeper maxSaves = topSaves.get(0);
System.out.printf("扑救王: %s (扑救数: %d)\n",
maxSaves.getName(), maxSaves.getSaves());
// 分析效率和忙碌度的关系
System.out.printf("\n综合评价:%s是本赛季最忙碌的门将,共做出%d次精彩扑救!\n",
busiest.getName(), busiest.getSaves());
if (busiest.getGoalsConceded() > 30) {
System.out.printf("注意:%s失球较多(%d球),说明防守压力大,"));
}
}
}
}
增强版:添加更多分析功能
// 统计分析类
class GoalkeeperAnalyzer {
// 计算扑救成功率
public static double getSaveRate(Goalkeeper gk) {
int totalShots = gk.getSaves() + gk.getGoalsConceded();
return totalShots > 0 ? (double) gk.getSaves() / totalShots * 100 : 0;
}
// 生成详细的球员报告
public static String generateDetailedReport(Goalkeeper gk) {
StringBuilder report = new StringBuilder();
report.append(String.format("===== %s (%s) 详细报告 =====\n",
gk.getName(), gk.getTeam()));
report.append(String.format("扑救次数: %d\n", gk.getSaves()));
report.append(String.format("失球数: %d\n", gk.getGoalsConceded()));
report.append(String.format("解围次数: %d\n", gk.getClearances()));
report.append(String.format("接球次数: %d\n", gk.getCatches()));
report.append(String.format("扑救成功率: %.1f%%\n", getSaveRate(gk)));
report.append(String.format("忙碌指数: %.1f\n", gk.getBusynessScore()));
// 评价
if (gk.getSaves() > 150) {
report.append("\n评价: 🌟 世界级门将,防守范围大,反应神速");
} else if (gk.getSaves() > 100) {
report.append("\n评价: ⭐ 优秀门将,表现稳定");
} else {
report.append("\n评价: 👍 表现不错,仍需提升");
}
return report.toString();
}
}
// 更新主类以包含更多功能
public class Main {
public static void main(String[] args) {
// 运行原有demo
GoalkeeperBusynessDemo.main(args);
// 额外演示详细报告
System.out.println("\n\n" + "=".repeat(50));
System.out.println("详细球员报告示例:");
System.out.println("=".repeat(50));
// 创建两个门将进行对比
Goalkeeper gkA = new Goalkeeper("德赫亚", "曼联");
Goalkeeper gkB = new Goalkeeper("拉姆斯代尔", "阿森纳");
// 模拟一些数据
for (int i = 0; i < 100; i++) {
gkA.recordSave();
if (i % 3 == 0) gkA.recordGoalConceded();
if (i % 4 == 0) gkA.recordClearance();
if (i % 5 == 0) gkA.recordCatch();
}
for (int i = 0; i < 80; i++) {
gkB.recordSave();
if (i % 2 == 0) gkB.recordGoalConceded();
if (i % 3 == 0) gkB.recordClearance();
if (i % 6 == 0) gkB.recordCatch();
}
System.out.println(GoalkeeperAnalyzer.generateDetailedReport(gkA));
System.out.println("\n");
System.out.println(GoalkeeperAnalyzer.generateDetailedReport(gkB));
// 输出对比结论
System.out.println("\n结论:");
if (gkA.getBusynessScore() > gkB.getBusynessScore()) {
System.out.printf("%s比%s更忙,但%s的扑救成功率更高!\n",
gkA.getName(), gkB.getName(),
GoalkeeperAnalyzer.getSaveRate(gkA) > GoalkeeperAnalyzer.getSaveRate(gkB)
? gkA.getName() : gkB.getName());
} else {
System.out.printf("%s比%s更忙,但%s的扑救成功率更高!\n",
gkB.getName(), gkA.getName(),
GoalkeeperAnalyzer.getSaveRate(gkB) > GoalkeeperAnalyzer.getSaveRate(gkA)
? gkB.getName() : gkA.getName());
}
}
}
运行效果示例:
⚽ 开始模拟整个赛季的比赛...
已完成 10 场比赛...
已完成 20 场比赛...
已完成 30 场比赛...
赛季结束!生成统计报告:
========== 门将忙碌度统计报告 ==========
门将 球队 扑救 失球 解围 接球 忙碌指数
----------------------------------------------------------------------
诺伊尔 拜仁慕尼黑 153 28 89 120 158.6
多纳鲁马 巴黎圣日耳曼 142 25 78 115 145.8
库尔图瓦 皇家马德里 135 22 85 108 139.7
特尔施特根 巴塞罗那 128 30 72 98 131.2
阿利松 利物浦 118 27 68 92 120.4
埃德森 曼城 95 18 55 85 101.7
----------------------------------------------------------------------
🏆 最忙门将: 诺伊尔 (球队: 拜仁慕尼黑)
扑救次数: 153, 忙碌指数: 158.6
这个Java案例完整展示了:
- 门将类设计 - 包含所有相关属性和方法
- 比赛模拟 - 随机生成门将活动
- 统计分析 - 计算忙碌指数
- 排名比较 - 找出最忙门将
- 详细报告 - 生成完整的统计报告
程序模拟了一个完整赛季来统计门将的工作量,综合考虑扑救、解围和接球次数来评估门将的忙碌程度。