所以我創建了一個簡單的基於文本的遊戲,但遇到了我的循環麻煩。所以我的角色與同一個敵人有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),以便您的代碼更易於理解。 –