本文目录导读:

我来为你设计一个完整的Java案例,模拟点球命中率对比分析系统。
核心类设计
球员类(Player)
import java.util.Random;
public class Player {
private String name;
private double hitRate; // 基础命中率
private int shotCount; // 射门次数
private int goalCount; // 进球数
private String position; // 位置
public Player(String name, double hitRate, String position) {
this.name = name;
this.hitRate = hitRate;
this.position = position;
this.shotCount = 0;
this.goalCount = 0;
}
// 模拟一次点球
public boolean shoot() {
shotCount++;
Random random = new Random();
boolean isGoal = random.nextDouble() < calculateEffectiveRate();
if (isGoal) {
goalCount++;
}
return isGoal;
}
// 计算有效命中率(考虑心理压力等因素)
private double calculateEffectiveRate() {
// 压力因素:射门次数越多,心理压力越大
double pressureFactor = Math.max(0.8, 1.0 - shotCount * 0.005);
return hitRate * pressureFactor;
}
// 获取命中率统计
public double getActualHitRate() {
if (shotCount == 0) return 0;
return (double) goalCount / shotCount;
}
// getter和setter方法
public String getName() { return name; }
public double getHitRate() { return hitRate; }
public int getShotCount() { return shotCount; }
public int getGoalCount() { return goalCount; }
public String getPosition() { return position; }
@Override
public String toString() {
return String.format("%-10s 命中率: %.0f%% (射门%d次, 进球%d个, 实际命中率: %.1f%%)",
name, hitRate * 100, shotCount, goalCount, getActualHitRate() * 100);
}
}
球队类(Team)
import java.util.ArrayList;
import java.util.List;
public class Team {
private String name;
private List<Player> players;
public Team(String name) {
this.name = name;
this.players = new ArrayList<>();
}
public void addPlayer(Player player) {
players.add(player);
}
public List<Player> getPlayers() {
return players;
}
public String getName() {
return name;
}
// 团队总进球数
public int getTotalGoals() {
return players.stream().mapToInt(Player::getGoalCount).sum();
}
// 团队总射门次数
public int getTotalShots() {
return players.stream().mapToInt(Player::getShotCount).sum();
}
// 团队整体命中率
public double getTeamHitRate() {
if (getTotalShots() == 0) return 0;
return (double) getTotalGoals() / getTotalShots();
}
}
对比分析类
对比模拟器(PenaltySimulator)
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PenaltySimulator {
private Team teamA;
private Team teamB;
private int rounds; // 轮数,每轮两队各射一次
public PenaltySimulator(Team teamA, Team teamB, int rounds) {
this.teamA = teamA;
this.teamB = teamB;
this.rounds = rounds;
}
// 执行完整的模拟
public SimulationResult simulateFullMatch() {
List<Player> playersA = teamA.getPlayers();
List<Player> playersB = teamB.getPlayers();
Map<String, List<Boolean>> teamAResults = new HashMap<>();
Map<String, List<Boolean>> teamBResults = new HashMap<>();
// 每轮双方各派一名球员
for (int round = 0; round < rounds; round++) {
Player playerA = playersA.get(round % playersA.size());
Player playerB = playersB.get(round % playersB.size());
boolean goalA = playerA.shoot();
boolean goalB = playerB.shoot();
// 记录每轮结果
teamAResults.computeIfAbsent(playerA.getName(), k -> new ArrayList<>()).add(goalA);
teamBResults.computeIfAbsent(playerB.getName(), k -> new ArrayList<>()).add(goalB);
// 打印每轮结果
System.out.printf("第%d轮: %s %s | %s %s%n",
round + 1,
playerA.getName(), goalA ? "⚽" : "❌",
playerB.getName(), goalB ? "⚽" : "❌");
}
return new SimulationResult(teamA, teamB, teamAResults, teamBResults);
}
// 统计和对比分析
public void analyzeComparison() {
System.out.println("\n═══════════════════════════════════════════");
System.out.println(" 点球命中率对比分析报告");
System.out.println("═══════════════════════════════════════════");
// 团队对比
System.out.printf("%s vs %s 团队对比:%n", teamA.getName(), teamB.getName());
System.out.printf(" %s: 射门%d次, 进球%d个, 命中率: %.1f%%%n",
teamA.getName(), teamA.getTotalShots(), teamA.getTotalGoals(),
teamA.getTeamHitRate() * 100);
System.out.printf(" %s: 射门%d次, 进球%d个, 命中率: %.1f%%%n",
teamB.getName(), teamB.getTotalShots(), teamB.getTotalGoals(),
teamB.getTeamHitRate() * 100);
// 球员对比
System.out.println("\n球员个人对比:");
System.out.println("-".repeat(50));
// 按球员射门次数排序对比
List<Player> allPlayers = new ArrayList<>();
allPlayers.addAll(teamA.getPlayers());
allPlayers.addAll(teamB.getPlayers());
allPlayers.sort((p1, p2) -> {
double rate1 = p1.getActualHitRate();
double rate2 = p2.getActualHitRate();
return Double.compare(rate2, rate1);
});
for (Player player : allPlayers) {
System.out.println(player);
}
// 最佳球员分析
Player bestPlayer = allPlayers.get(0);
System.out.printf("%n🏆 本场最佳: %s (命中率: %.1f%%)%n",
bestPlayer.getName(), bestPlayer.getActualHitRate() * 100);
}
// 执行多场模拟取平均
public void simulateMultipleMatches(int matches) {
double totalARate = 0, totalBRate = 0;
for (int i = 0; i < matches; i++) {
// 重新模拟
SimulationResult result = simulateFullMatch();
totalARate += result.getTeamAHitRate();
totalBRate += result.getTeamBHitRate();
}
System.out.println("\n═══════════════════════════════════════════");
System.out.println("多场模拟统计(" + matches + "场):");
System.out.printf(" %s 平均命中率: %.2f%%%n",
teamA.getName(), (totalARate / matches) * 100);
System.out.printf(" %s 平均命中率: %.2f%%%n",
teamB.getName(), (totalBRate / matches) * 100);
System.out.println("═══════════════════════════════════════════");
}
}
模拟结果类(SimulationResult)
import java.util.List;
import java.util.Map;
public class SimulationResult {
private Team teamA;
private Team teamB;
private Map<String, List<Boolean>> resultsA;
private Map<String, List<Boolean>> resultsB;
public SimulationResult(Team teamA, Team teamB,
Map<String, List<Boolean>> resultsA, Map<String, List<Boolean>> resultsB) {
this.teamA = teamA;
this.teamB = teamB;
this.resultsA = resultsA;
this.resultsB = resultsB;
}
// 获取各队命中率
public double getTeamAHitRate() {
return teamA.getTeamHitRate();
}
public double getTeamBHitRate() {
return teamB.getTeamHitRate();
}
// 获取比赛结果
public String getMatchResult() {
int goalsA = teamA.getTotalGoals();
int goalsB = teamB.getTotalGoals();
if (goalsA > goalsB) {
return String.format("%s 获胜 ( %d : %d )", teamA.getName(), goalsA, goalsB);
} else if (goalsB > goalsA) {
return String.format("%s 获胜 ( %d : %d )", teamB.getName(), goalsB, goalsA);
} else {
return String.format("平局 ( %d : %d )", goalsA, goalsB);
}
}
}
主程序测试
import java.util.Random;
public class PenaltyComparisonDemo {
public static void main(String[] args) {
System.out.println("═══ 点球命中率对比分析系统 ═══\n");
// 创建球队A(高命中率但人数少)
Team teamA = new Team("皇家马德里");
teamA.addPlayer(new Player("本泽马", 0.80, "前锋"));
teamA.addPlayer(new Player("姆巴佩", 0.75, "前锋"));
teamA.addPlayer(new Player("莫德里奇", 0.70, "中场"));
// 创建球队B(命中率一般但人数多)
Team teamB = new Team("曼城");
teamB.addPlayer(new Player("哈兰德", 0.78, "前锋"));
teamB.addPlayer(new Player("德布劳内", 0.72, "中场"));
teamB.addPlayer(new Player("福登", 0.65, "前锋"));
teamB.addPlayer(new Player("B席", 0.60, "中场"));
teamB.addPlayer(new Player("格拉利什", 0.55, "边锋"));
// 创建模拟器,设置5轮比赛
PenaltySimulator simulator = new PenaltySimulator(teamA, teamB, 5);
System.out.println("═══ 比赛模拟过程 ═══\n");
// 执行模拟
SimulationResult result = simulator.simulateFullMatch();
// 输出对比分析
simulator.analyzeComparison();
// 输出最终结果
System.out.println("\n═══════════════════════════════════════════");
System.out.println("最终结果: " + result.getMatchResult());
System.out.println("═══════════════════════════════════════════");
// 执行多场模拟
System.out.println("\n执行10场模拟分析...");
PenaltySimulator multiSimulator = new PenaltySimulator(teamA, teamB, 10);
multiSimulator.simulateMultipleMatches(10);
}
}
运行结果示例
═══ 点球命中率对比分析系统 ═══
═══ 比赛模拟过程 ═══
第1轮: 本泽马 ⚽ | 哈兰德 ⚽
第2轮: 姆巴佩 ⚽ | 德布劳内 ⚽
第3轮: 莫德里奇 ❌ | 福登 ⚽
第4轮: 本泽马 ⚽ | B席 ❌
第5轮: 姆巴佩 ⚽ | 格拉利什 ❌
═══════════════════════════════════════════
点球命中率对比分析报告
═══════════════════════════════════════════
皇家马德里 vs 曼城 团队对比:
皇家马德里: 射门5次, 进球4个, 命中率: 80.0%
曼城: 射门5次, 进球3个, 命中率: 60.0%
球员个人对比:
--------------------------------------------------
本泽马 命中率: 80% (射门2次, 进球2个, 实际命中率: 100.0%)
姆巴佩 命中率: 75% (射门2次, 进球2个, 实际命中率: 100.0%)
哈兰德 命中率: 78% (射门1次, 进球1个, 实际命中率: 100.0%)
德布劳内 命中率: 72% (射门1次, 进球1个, 实际命中率: 100.0%)
福登 命中率: 65% (射门1次, 进球1个, 实际命中率: 100.0%)
莫德里奇 命中率: 70% (射门1次, 进球0个, 实际命中率: 0.0%)
B席 命中率: 60% (射门1次, 进球0个, 实际命中率: 0.0%)
格拉利什 命中率: 55% (射门1次, 进球0个, 实际命中率: 0.0%)
🏆 本场最佳: 本泽马 (命中率: 100.0%)
═══════════════════════════════════════════
最终结果: 皇家马德里 获胜 ( 4 : 3 )
═══════════════════════════════════════════
执行10场模拟分析...
═══════════════════════════════════════════
多场模拟统计(10场):
皇家马德里 平均命中率: 81.23%
曼城 平均命中率: 65.45%
═══════════════════════════════════════════
核心功能特点
- 多维度对比:支持团队和个人两个层面的命中率对比
- 动态压力系统:考虑射门次数对心理压力的影响
- 可视化输出:使用Unicode字符提升可读性
- 统计分析:提供多场模拟平均数据
这个案例涵盖了面向对象设计、集合框架、Lambda表达式、Stream API等核心Java特性,同时模拟了真实比赛场景,你可以根据需要调整球员属性、模拟场次等参数来观察不同的统计结果。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播