2016-10-03 42 views
0

所以我創建了一個簡單的基於文本的遊戲,但遇到了我的循環麻煩。所以我的角色與同一個敵人有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; 
       } 
      } 
     } 
+0

似乎有些碼不顯示,如。 'characterHasntDied'定義在哪裏?你可以在運行代碼時包含這個和實際的輸出。 – dave

+0

你在戰鬥後重置了值(hp,characterHasntDied等)?請提供所有重要代碼,請參閱http://stackoverflow.com/help/mcve。例如,刪除代碼中不會更改值的部分(如System.out.printlns),以便您的代碼更易於理解。 –

回答

0

不要使用break,因爲它會擺脫其loop

if (enemyHP < 1) { 
    /*... */ 

    } else if (characterHP < 1) { 
    /*... */ 
    } 

模擬兩個玩家之間的三大戰役會是這樣

Scanner keyboard = new Scanner(System.in); 
Random rand = new Random(); 
int PowerPlayer1 =200; 
int PowerPlayer2 = 200; 
boolean winner=false; 
int ATKPlaayer1 = 0, ATKPlaayer2 = 0; 
String Player1 = "Rogue"; 
String Player2 = "Goblin";  

for (int i = 1; i <= 3; i++) { // i many battles 

     while(!winner) /*while there is no winner*/ 
     { 
      /* The Random method can improve it*/ 
      ATKPlaayer1 = rand.nextInt(200); 
      System.out.println(Player1+ " Attack : " + ATKPlaayer1); 
      PowerPlayer2-= ATKPlaayer1; 
      System.out.println(Player2 + " Power Diminished by the Attack "+ Player1 + " of "+ PowerPlayer2); 
      ATKPlaayer2 = rand.nextInt(200); 
      System.out.println(Player2+ " Attack : " + ATKPlaayer2); 
      PowerPlayer1-=ATKPlaayer2; 
      System.out.println(Player1 + " Power Diminished by the Attack "+ Player2 + " of "+ PowerPlayer1); 
      if(PowerPlayer1<=0) 
      { 
       System.out.println(Player2 + " has Winner"); 
       winner=true; 
      } 
      else if(PowerPlayer2<=0) 
      { 
       System.out.println(Player1 + " has Winner"); 
       winner=true; 
      } 

     } 
     /* Reset Variables */ 
     winner = false; 
     PowerPlayer1 =200; 
     PowerPlayer2 =200; 
     } 
+0

一旦條件滿足,我該如何結束循環?例如,在我的輸出: 與ATK = 6 + 4 = 10 盜賊HP地精攻擊現在是34 - 10 = 24次 盜賊用ATK = 3 + 8 = 11 地精HP攻擊現在14 - 11 = 3次 與ATK = 6 + 4 = 10 盜賊HP地精攻擊現在是24 - 10現在= 14次 盜賊用ATK = 3 + 8 = 11 地精HP攻擊是3 - 11 = -8 **端此處** \ n **而不是在這裏允許額外的敵人攻擊** 地精攻擊與ATK = 6 + 4 = 10 盜賊HP現在是14 - 10 = 4 盜賊擊敗了Goblin 2 ***流氓與地精戰役3 *** –

+0

休息退出論壇,讓第一次迭代你這樣做:) –

+0

希望我的回答幫助更新。 –