所以我创建了一个简单的基于文本的游戏,但遇到了我的循环麻烦。所以我的角色与同一个敌人有3次战斗,但是直到一个角色死亡,战斗才会结束。使用我现在的代码,它只会在第一场战斗中循环,但会将战斗2和战斗3留空。 HP从战斗转移到战斗,但敌人HP在每次随后的战斗中重置为25。 当角色死亡时,我该如何让我的环路立即停止? 我在做什么错?简单的基于Java文本的游戏循环
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
int characterHP = 0, characterStrength = 0, weaponMin = 0, weaponMax = 0;
int enemyHP = 0, enemyStrength = 0, enemyWeaponMin = 0, enemyWeaponMax = 0;
boolean running = true, characterHasntDied = true, enemyHasntDied = true;
int choice;
String characterChoice = null;
String enemyChoice = null;
int ATK = 0, enemyATK = 0;
for (int i = 1; i <= 3; i++) {
ATK = rand.nextInt((weaponMax - weaponMin) + 1) + weaponMin;
enemyATK = rand.nextInt((enemyWeaponMax - enemyWeaponMin) + 1) + enemyWeaponMin;
int valueOfAttack = (ATK + characterStrength);
int enemyValueOfAttack = (enemyATK + enemyStrength);
enemyHP -= valueOfAttack;
characterHP -= enemyValueOfAttack;
if (enemyHP < 1) {
characterHasntDied = false;
System.out.println(characterChoice + " has defeated Goblin " + i);
break;
} else if (characterHP < 1) {
characterHasntDied = false;
System.out.println(characterChoice + " is defeated in battle!");
break;
}
}
}
似乎有些码不显示,如。 'characterHasntDied'定义在哪里?你可以在运行代码时包含这个和实际的输出。 – dave
你在战斗后重置了值(hp,characterHasntDied等)?请提供所有重要代码,请参阅http://stackoverflow.com/help/mcve。例如,删除代码中不会更改值的部分(如System.out.printlns),以便您的代码更易于理解。 –