本文目录导读:

我来为你设计一个Java案例,模拟足球比赛中统计球员使用假动作晃过防守球员的次数。
完整示例代码
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* 足球假动作统计系统
*/
public class FootballDribbleSimulator {
// 假动作类型枚举
enum FeintType {
BODY_SWERVE("身体晃动", 0.8), // 成功率80%
STEP_OVER("踩单车", 0.7), // 成功率70%
CUT_BACK("急停变向", 0.75), // 成功率75%
CRUYFF_TURN("克鲁伊夫转身", 0.65),// 成功率65%
RONALDO_CHOP("罗纳尔多切球", 0.72); // 成功率72%
private String desc;
private double successRate;
FeintType(String desc, double successRate) {
this.desc = desc;
this.successRate = successRate;
}
public String getDesc() {
return desc;
}
public double getSuccessRate() {
return successRate;
}
}
// 球员类
static class Player {
private String name;
private int dribbleSkill; // 带球能力 1-100
private int speed; // 速度 1-100
private int balance; // 平衡性 1-100
public Player(String name, int dribbleSkill, int speed, int balance) {
this.name = name;
this.dribbleSkill = dribbleSkill;
this.speed = speed;
this.balance = balance;
}
public String getName() {
return name;
}
public int getDribbleSkill() {
return dribbleSkill;
}
public int getSpeed() {
return speed;
}
public int getBalance() {
return balance;
}
@Override
public String toString() {
return String.format("%s(带球:%d 速度:%d 平衡:%d)",
name, dribbleSkill, speed, balance);
}
}
// 比赛模拟器
static class MatchSimulator {
private Player player; // 重点关注的球员
private List<String> eventLog; // 事件日志
private Random random;
private int totalFeints = 0; // 假动作总数
private int successfulFeints = 0; // 成功晃过防守次数
private int failedFeints = 0; // 假动作失败次数
public MatchSimulator(Player player) {
this.player = player;
this.eventLog = new ArrayList<>();
this.random = new Random();
}
// 模拟单次进攻
public void simulateAttack(int defenseLevel) {
// 初始化本回合数据
int attackFeints = 0;
int attackSuccess = 0;
eventLog.add("\n=== 新进攻回合 ===");
eventLog.add("球员: " + player.getName());
// 每次进攻尝试进行几次假动作
int maxAttempts = random.nextInt(4) + 1; // 1-4次假动作机会
for (int i = 0; i < maxAttempts; i++) {
// 随机选择假动作类型
FeintType type = FeintType.values()[
random.nextInt(FeintType.values().length)
];
// 计算成功率
double successProb = calculateSuccessProbability(type, defenseLevel);
boolean success = random.nextDouble() < successProb;
attackFeints++;
if (success) {
attackSuccess++;
successfulFeints++;
eventLog.add(String.format(
"✓ 第%d次: 使用【%s】成功晃过防守球员!",
attackFeints, type.getDesc()
));
} else {
failedFeints++;
eventLog.add(String.format(
"✗ 第%d次: 使用【%s】被防守球员拦截",
attackFeints, type.getDesc()
));
// 如果被拦截,进攻结束
break;
}
}
totalFeints += attackFeints;
eventLog.add(String.format(
"本回合结果: 假动作%d次, 成功晃过%d次",
attackFeints, attackSuccess
));
}
// 计算成功概率
private double calculateSuccessProbability(FeintType type, int defenseLevel) {
double baseRate = type.getSuccessRate();
// 球员能力加成
double skillBonus = (player.getDribbleSkill() - 50) / 100.0 * 0.3;
double speedBonus = (player.getSpeed() - 50) / 100.0 * 0.15;
double balanceBonus = (player.getBalance() - 50) / 100.0 * 0.15;
// 防守强度削减
double defensePenalty = defenseLevel / 200.0;
// 计算最终概率
double prob = baseRate + skillBonus + speedBonus + balanceBonus - defensePenalty;
// 限制在5%-95%之间
return Math.max(0.05, Math.min(0.95, prob));
}
// 打印统计报告
public void printReport() {
System.out.println("\n========== 比赛统计报告 ==========");
System.out.println("球员档案: " + player);
System.out.println("-----------------------------------");
System.out.println("【假动作统计】");
System.out.printf("总尝试次数: %d\n", totalFeints);
System.out.printf("成功晃过次数: %d\n", successfulFeints);
System.out.printf("失败次数: %d\n", failedFeints);
if (totalFeints > 0) {
double successRate = (successfulFeints * 100.0) / totalFeints;
System.out.printf("成功率: %.1f%%\n", successRate);
}
System.out.println("-----------------------------------");
System.out.println("【详细事件记录】");
for (String event : eventLog) {
System.out.println(event);
}
System.out.println("===================================");
}
// 重置统计数据
public void reset() {
totalFeints = 0;
successfulFeints = 0;
failedFeints = 0;
eventLog.clear();
}
}
// 主测试类
public static void main(String[] args) {
System.out.println("=== 足球假动作晃人统计系统 ===\n");
// 创建球员
Player messi = new Player("梅西", 95, 90, 85);
Player ronaldo = new Player("C罗", 85, 88, 82);
Player normalPlayer = new Player("普通球员", 60, 65, 58);
// 创建模拟器
MatchSimulator simulator = new MatchSimulator(messi);
// 模拟多场比赛和进攻
System.out.println("开始模拟梅西的比赛...\n");
// 模拟10次进攻,防守等级不同
int defenseLevels[] = {5, 7, 6, 8, 5, 7, 6, 9, 5, 6};
for (int i = 0; i < 10; i++) {
simulator.simulateAttack(defenseLevels[i]);
}
// 打印报告
simulator.printReport();
// 模拟C罗的比赛
System.out.println("\n\n开始模拟C罗的比赛...\n");
simulator = new MatchSimulator(ronaldo);
for (int i = 0; i < 5; i++) {
simulator.simulateAttack(6);
}
simulator.printReport();
// 对比分析
System.out.println("\n\n========== 球员对比分析 ==========");
comparePlayers();
}
// 对比多个球员
public static void comparePlayers() {
Player[] players = {
new Player("梅西", 95, 90, 85),
new Player("C罗", 85, 88, 82),
new Player("姆巴佩", 80, 98, 75)
};
for (Player p : players) {
MatchSimulator sim = new MatchSimulator(p);
// 每个球员模拟20次进攻
for (int i = 0; i < 20; i++) {
sim.simulateAttack(7);
}
// 直接获取统计信息(需要添加getter方法)
System.out.printf("%s: 假动作次数=%d, 成功晃过=%d, 成功率=%.1f%%\n",
p.getName(),
sim.getTotalFeints(),
sim.getSuccessfulFeints(),
sim.getTotalFeints() > 0 ?
(sim.getSuccessfulFeints() * 100.0 / sim.getTotalFeints()) : 0
);
}
}
}
补充:添加getter方法
// 在MatchSimulator类中添加以下getter方法
public int getTotalFeints() {
return totalFeints;
}
public int getSuccessfulFeints() {
return successfulFeints;
}
public int getFailedFeints() {
return failedFeints;
}
运行结果示例
=== 足球假动作晃人统计系统 ===
开始模拟梅西的比赛...
========== 比赛统计报告 ==========
球员档案: 梅西(带球:95 速度:90 平衡:85)
-----------------------------------
【假动作统计】
总尝试次数: 24
成功晃过次数: 19
失败次数: 5
成功率: 79.2%
-----------------------------------
【详细事件记录】
=== 新进攻回合 ===
球员: 梅西
✓ 第1次: 使用【身体晃动】成功晃过防守球员!
✓ 第2次: 使用【踩单车】成功晃过防守球员!
本回合结果: 假动作2次, 成功晃过2次
...
===================================
核心特性
- 假动作类型:包含5种不同的假动作,每种有不同的基础成功率
- 球员属性:带球能力、速度、平衡性影响假动作成功率
- 防守强度:防守等级影响成功率
- 详细日志:记录每次假动作的使用情况
- 统计功能:统计总次数、成功次数、失败次数和成功率
这个系统可以扩展到更多功能,如实时UI显示、数据库存储、多人对战等。