2016-02-25 22 views
-2

所以,我試圖製作一個基於文本的RPG。一個好的首發項目,因爲我是所有這些的新手。我想打一個關鍵命中,而數學。隨機似乎是最好的方法。只有一個問題;當它循環時,它不刷新隨機數。我可以假設那是因爲它調用了一次,一旦它被調用,就是這樣。這就是價值。我想知道一個替代方案,以便每當while循環重新開始時刷新它。我知道我的代碼中還有一些其他的錯誤;但這不是我現在關注的。這裏是整個方法:Java Math.random不會在while循環上刷新(當然)

public void battleStart(Enemy enemy, Player player) 
    { 

     while (enemy.health != 0) 
     { 
      String battle = null; 

      int criticalHt = (int) (Math.random() * 10) + 0; 

      boolean T = true; 
      while (T = true) 
      { 

       battle = Game.console.next(); 

       enemy.attackplayer(player); 

       if (battle.toLowerCase().contains("skip")) 
       { 

        System.out.println(criticalHt); 

        if (criticalHt == 1) 
        { 

         player.hitPoints -= enemy.dmg * 2; 

         System.out.println(Game.toTitleCase(enemy.firstName) + " " + Game.toTitleCase(enemy.lastName) + " has used " + Game.toTitleCase(enemy.attackName) + " for a critical level of " + enemy.dmg * 2 + "!.\n Your health is now " + player.hitPoints + "."); 

        } 

        else 
        { 

         player.hitPoints -= enemy.dmg; 

         System.out.println(Game.toTitleCase(enemy.firstName) + " " + Game.toTitleCase(enemy.lastName) + " has used " + Game.toTitleCase(enemy.attackName) + ".\n Your health is now " + player.hitPoints + "."); 

         System.out.println("Your turn to attack! Use an attack, or type \"skip\" to let your enemy strike."); 
        }; 
        if (battle.contains("skp")) 
        { 

        }; 

       } 
      } 
     } 
+0

見http://stackoverflow.com/questions/7961788/math-random-explained –

+3

我知道你不想聽到關於其他錯誤的消息,但是當(T = true)每次都將T賦予T時,你可能需要(T) –

回答

1

移動代碼

int criticalHt = (int)(Math.random() * 10) + 0; 

內while循環

while(T = true){ 
    int criticalHt = (int)(Math.random() * 10) + 0; 
    battle = Game.console.next(); 
    ... 
+0

Ahhhhh ninja'd。 :P – ifly6

+0

要麼你應該治癒癌症,要麼我的智商正好是78。 –