本文目录导读:

我为您创建一个Java案例来统计地面对抗(Ground Combat)的胜负,这个案例将模拟两个战斗单位之间的地面战斗。
完整的地面对抗统计系统
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class GroundCombatSimulator {
// 战斗单位类
static class CombatUnit {
private String name;
private int health;
private int maxHealth;
private int attackPower;
private int defense;
private int hitChance; // 命中率百分比
private int criticalChance; // 暴击率百分比
private int wins;
private int losses;
public CombatUnit(String name, int health, int attackPower, int defense, int hitChance, int criticalChance) {
this.name = name;
this.health = health;
this.maxHealth = health;
this.attackPower = attackPower;
this.defense = defense;
this.hitChance = hitChance;
this.criticalChance = criticalChance;
this.wins = 0;
this.losses = 0;
}
// 重置战斗状态
public void reset() {
this.health = this.maxHealth;
}
// 获取当前单位信息
public String getStats() {
return String.format("%s [HP: %d/%d, ATK: %d, DEF: %d, 命中: %d%%, 暴击: %d%%]",
name, health, maxHealth, attackPower, defense, hitChance, criticalChance);
}
public String getName() { return name; }
public int getHealth() { return health; }
public int getMaxHealth() { return maxHealth; }
public int getWins() { return wins; }
public int getLosses() { return losses; }
public void addWin() { wins++; }
public void addLoss() { losses++; }
// 受到伤害
public void takeDamage(int damage) {
int reducedDamage = Math.max(0, damage - defense);
health = Math.max(0, health - reducedDamage);
}
// 攻击敌人
public int attack(CombatUnit enemy, Random random) {
// 检查命中
if (random.nextInt(100) >= hitChance) {
return 0; // 未命中
}
int damage = attackPower;
// 检查暴击
if (random.nextInt(100) < criticalChance) {
damage *= 2; // 暴击双倍伤害
}
// 添加一些随机性
damage += random.nextInt(attackPower / 4);
damage -= random.nextInt(attackPower / 8);
damage = Math.max(1, damage);
enemy.takeDamage(damage);
return damage;
}
public boolean isAlive() {
return health > 0;
}
}
// 战斗统计类
static class CombatStatistics {
private int totalBattles;
private int fighter1Wins;
private int fighter2Wins;
private int draws;
private int totalRounds;
private int maxRounds;
private int minRounds;
private int totalDamageDealt;
public CombatStatistics() {
this.totalBattles = 0;
this.fighter1Wins = 0;
this.fighter2Wins = 0;
this.draws = 0;
this.totalRounds = 0;
this.maxRounds = 0;
this.minRounds = Integer.MAX_VALUE;
this.totalDamageDealt = 0;
}
public void recordBattle(int winner, int rounds, int damage) {
totalBattles++;
totalRounds += rounds;
totalDamageDealt += damage;
if (winner == 1) fighter1Wins++;
else if (winner == 2) fighter2Wins++;
else draws++;
if (rounds > maxRounds) maxRounds = rounds;
if (rounds < minRounds) minRounds = rounds;
}
public double getAverageRounds() {
return totalBattles > 0 ? (double) totalRounds / totalBattles : 0;
}
public double getAverageDamage() {
return totalBattles > 0 ? (double) totalDamageDealt / totalBattles : 0;
}
public double getWinRate(int fighter) {
int wins = fighter == 1 ? fighter1Wins : fighter2Wins;
return totalBattles > 0 ? (double) wins / totalBattles * 100 : 0;
}
public void printStatistics(CombatUnit unit1, CombatUnit unit2) {
System.out.println("\n========== 战斗统计报告 ==========");
System.out.println("总战斗场次: " + totalBattles);
System.out.println("------------------------------------");
System.out.println(unit1.getName() + " 获胜次数: " + fighter1Wins);
System.out.println(unit2.getName() + " 获胜次数: " + fighter2Wins);
System.out.println("平局次数: " + draws);
System.out.println("------------------------------------");
System.out.println("平均回合数: " + String.format("%.2f", getAverageRounds()));
System.out.println("最少回合数: " + (minRounds == Integer.MAX_VALUE ? 0 : minRounds));
System.out.println("最多回合数: " + maxRounds);
System.out.println("平均伤害值: " + String.format("%.2f", getAverageDamage()));
System.out.println("====================================");
}
}
// 主要模拟方法
public static void simulateBattle(CombatUnit unit1, CombatUnit unit2, int battleCount, boolean verbose) {
Random random = new Random();
CombatStatistics stats = new CombatStatistics();
System.out.println("\n========== 地面对抗模拟开始 ==========");
System.out.println("战斗方1: " + unit1.getStats());
System.out.println("战斗方2: " + unit2.getStats());
System.out.println("模拟场次: " + battleCount);
System.out.println("======================================");
for (int battle = 1; battle <= battleCount; battle++) {
unit1.reset();
unit2.reset();
int round = 0;
int damageDealt = 0;
int winner = 0;
if (verbose) {
System.out.println("\n--- 第 " + battle + " 场战斗 ---");
}
while (unit1.isAlive() && unit2.isAlive() && round < 100) {
round++;
// 轮流攻击
int damage1 = unit1.attack(unit2, random);
damageDealt += damage1;
if (unit2.isAlive()) {
int damage2 = unit2.attack(unit1, random);
damageDealt += damage2;
}
if (verbose) {
System.out.println("回合 " + round + ": ");
System.out.println(" " + unit1.getName() + " 攻击 " + unit2.getName() +
(damage1 > 0 ? " 造成 " + damage1 + " 点伤害" : " 未命中"));
System.out.println(" " + unit2.getName() + " 攻击 " + unit1.getName() +
(damage2 > 0 ? " 造成 " + damage2 + " 点伤害" : " 未命中"));
System.out.println(" " + unit1.getName() + " HP: " + unit1.getHealth() +
" | " + unit2.getName() + " HP: " + unit2.getHealth());
}
if (round >= 100) {
System.out.println("战斗达到回合上限,判定为平局");
winner = 0;
} else if (!unit1.isAlive() && !unit2.isAlive()) {
winner = 0; // 平局
} else if (!unit1.isAlive()) {
winner = 2;
} else if (!unit2.isAlive()) {
winner = 1;
}
}
// 记录胜负
if (winner == 1) {
unit1.addWin();
unit2.addLoss();
if (verbose) System.out.println("★★★ " + unit1.getName() + " 获胜!");
} else if (winner == 2) {
unit2.addWin();
unit1.addLoss();
if (verbose) System.out.println("★★★ " + unit2.getName() + " 获胜!");
} else {
if (verbose) System.out.println("★★★ 平局!");
}
stats.recordBattle(winner, round, damageDealt);
}
// 打印统计结果
System.out.println("\n========== 最终统计结果 ==========");
System.out.println(unit1.getName() + " - 胜场: " + unit1.getWins() +
" | 负场: " + unit1.getLosses() +
" | 胜率: " + String.format("%.1f%%",
(double) unit1.getWins() / battleCount * 100));
System.out.println(unit2.getName() + " - 胜场: " + unit2.getWins() +
" | 负场: " + unit2.getLosses() +
" | 胜率: " + String.format("%.1f%%",
(double) unit2.getWins() / battleCount * 100));
stats.printStatistics(unit1, unit2);
// 判定最终胜者
if (unit1.getWins() > unit2.getWins()) {
System.out.println("\n🎉 最终胜者: " + unit1.getName() + "!");
} else if (unit2.getWins() > unit1.getWins()) {
System.out.println("\n🎉 最终胜者: " + unit2.getName() + "!");
} else {
System.out.println("\n🤝 双方势均力敌,打成平手!");
}
}
// 主程序入口
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
System.out.println("欢迎来到地面对抗模拟系统!");
System.out.println("===============================");
// 预设战斗单位
List<CombatUnit> predefinedUnits = new ArrayList<>();
predefinedUnits.add(new CombatUnit("步兵", 100, 35, 10, 85, 10));
predefinedUnits.add(new CombatUnit("坦克", 150, 50, 30, 75, 5));
predefinedUnits.add(new CombatUnit("装甲车", 120, 40, 20, 80, 8));
predefinedUnits.add(new CombatUnit("狙击手", 70, 70, 5, 70, 30));
predefinedUnits.add(new CombatUnit("机器人", 130, 45, 25, 82, 12));
System.out.println("请选择战斗单位:");
for (int i = 0; i < predefinedUnits.size(); i++) {
System.out.println((i + 1) + ". " + predefinedUnits.get(i).getStats());
}
System.out.println("6. 自定义单位");
System.out.print("\n请选择第一个单位: ");
int choice1 = getValidChoice(scanner, 1, 6);
CombatUnit unit1 = selectUnit(scanner, predefinedUnits, choice1);
System.out.print("请选择第二个单位: ");
int choice2 = getValidChoice(scanner, 1, 6);
CombatUnit unit2 = selectUnit(scanner, predefinedUnits, choice2);
System.out.print("请输入模拟场次 (1-1000): ");
int battleCount = Math.max(1, Math.min(1000, scanner.nextInt()));
System.out.print("是否显示详细战斗过程?(y/n): ");
scanner.nextLine();
boolean verbose = scanner.nextLine().equalsIgnoreCase("y");
// 开始模拟
simulateBattle(unit1, unit2, battleCount, verbose);
scanner.close();
}
private static int getValidChoice(Scanner scanner, int min, int max) {
int choice;
while (true) {
try {
choice = scanner.nextInt();
if (choice >= min && choice <= max) {
return choice;
}
System.out.print("无效选择,请输入 " + min + "-" + max + " 之间的数字: ");
} catch (Exception e) {
System.out.print("请输入有效数字: ");
scanner.nextLine();
}
}
}
private static CombatUnit selectUnit(Scanner scanner, List<CombatUnit> units, int choice) {
if (choice <= units.size()) {
return units.get(choice - 1);
}
// 自定义单位
System.out.println("\n创建自定义单位:");
scanner.nextLine();
System.out.print("单位名称: ");
String name = scanner.nextLine();
System.out.print("生命值 (50-200): ");
int health = Math.max(50, Math.min(200, scanner.nextInt()));
System.out.print("攻击力 (20-80): ");
int attack = Math.max(20, Math.min(80, scanner.nextInt()));
System.out.print("防御力 (0-50): ");
int defense = Math.max(0, Math.min(50, scanner.nextInt()));
System.out.print("命中率 (50-100): ");
int hitChance = Math.max(50, Math.min(100, scanner.nextInt()));
System.out.print("暴击率 (0-50): ");
int criticalChance = Math.max(0, Math.min(50, scanner.nextInt()));
scanner.nextLine();
return new CombatUnit(name, health, attack, defense, hitChance, criticalChance);
}
}
使用方法
-
编译运行:
javac GroundCombatSimulator.java java GroundCombatSimulator
-
程序特点:
- 支持5种预定义单位和自定义单位
- 可设置模拟场次(最多1000场)
- 可选择详细或简洁输出模式
- 统计胜率、平均回合数、平均伤害等数据
功能特性
- 战斗系统:命中率、暴击率、防御减伤
- 统计功能:多维度统计战斗数据
- 灵活性:可自定义单位和参数
- 可视化:详细显示每回合战斗过程
这个案例完整实现了地面对抗的统计和模拟功能,可以根据实际需求调整参数。
版权声明: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直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播