2016-06-20 15 views
0

私は戦闘シーケンスを作ろうとしていますが、実際の戦闘を繰り返す必要があります。しかし、HPとenemyHPは、ランダムに生成された数値から最初のループの後に合計を引くようには見えません。ループが適用された後にランダムに生成された数値は機能しません

これが意味をなさない場合は申し訳ありません。私は物事を説明するのは本当に良いではないよ...

ここではコードです:

import java.util.Random; 
    import java.util.Scanner; 

    public class Hayyan { 

     public int hp = 100; 
     public int choice = 0; 

      public static void main(String[] args) { 

       combatHayyan bob = new combatHayyan(); 
       Hayyan junl = new Hayyan(); 

       while(junl.choice < 10){  
        System.out.println("Choose a weapon to attack with, type sword for a sword, or axe for an axe "); 
        bob.attack(); 
        bob.defend(); 
        System.out.println(); 

        if(junl.hp < 0 || bob.enemyHP < 0){ 
          junl.choice = 10; 
        } 

      } 
     } 
    } 

class combatHayyan { 

public int enemyHP = 50; 
public int yourTotal; 

public void attack(){ 

    weapons weapon = new weapons(); 

    Scanner bob = new Scanner(System.in); 
    switch(bob.nextLine()){ 

    case "sword": 

     weapon.sword(); 
     System.out.println("Enemy now has " + weapon.total + "HP left!"); 
     break; 


    case "axe": 

     weapon.axe(); 
     System.out.println("Enemy now has " + weapon.total + "HP left!"); 
     break; 

    } 
} 

public void defend(){ 

    Hayyan lost = new Hayyan(); 

    Random bob = new Random(); 
    int randomness = bob.nextInt(11) + 10; 
    yourTotal = lost.hp - randomness; 
    System.out.println("You now have " + yourTotal + "HP left!"); 

    } 
    } 

class weapons { 

public int total; 

public void sword(){ 

    int bob = 5 + (int)(Math.random() * ((10 - 5) + 1)); 
    combatHayyan llama = new combatHayyan(); 
    total = llama.enemyHP - bob; 

} 

public void axe(){ 

    int randomGenerate = 5 + (int)(Math.random() * ((10 - 5) + 1)); 
    combatHayyan llama = new combatHayyan(); 
    total = llama.enemyHP - randomGenerate; 


} 

}

答えて

0

あなたの質問は少し広いが、私はあなたがこれを変更する必要があると考えている:

は、これで
total = llama.enemyHP - bob; 

total = total - bob; 

最初にllama.enemyHPに合計を初期化するだけです。

yourTotal = lost.hp - randomness; 

をし、私はあなたがむしろほしいと信じている:を守るために同じこと、あなたがやっている

yourTotal = yourTotal - randomness; 

そうでない場合は、お使いの変数は常にループ内のすべてのパスで再計算されます。 私の推薦では、意味をなさないようにコードをリファクタリングする必要がありますが、基本的には、値をランダム値で変更しないので、ランダムな計算を何度も何度もやり直しています。

編集: あなたのコードをリファクタリングし、より多くのオブジェクト指向の概念を使用することを検討する必要があります。これを見て、デザインからあまり遠ざかることなくリファクタリングしたので、それに従ってソリューションと比較することができます:

class Hayyan { 
    public static void main(String[] args) { 
     CombatHayyan combatHayyan = new CombatHayyan(); 

     Scanner scanner = new Scanner(System.in); 
     while (combatHayyan.bothCombatantsAlive()) { 
      System.out.println("Choose a weapon to attack with, type sword for a sword, or axe for an axe "); 
      combatHayyan.attack(scanner); 
      combatHayyan.defend(); 
      System.out.println(); 
     } 

     scanner.close(); 
     combatHayyan.printWinner(); 
    } 
} 

class CombatHayyan { 
    public int enemyHP = 50; 
    public int yourHp = 100; 

    Weapons weapon = new Weapons(); 

    public void attack(Scanner scanner) { 
     int damage = 0; 
     switch (scanner.nextLine()) { 
     case "sword": 
      damage = weapon.sword(); 
      break; 

     case "axe": 
      damage = weapon.axe(); 
      break; 
     } 
     enemyHP = enemyHP - damage; 
     System.out.println("Enemy now has " + enemyHP + "HP left!"); 
    } 

    public void printWinner() { 
     String winner = yourHp>0?"You":"The enemy"; 
     int hp = yourHp>0?yourHp:enemyHP; 
     System.out.println(winner + " won! with " + hp + "HP remaining"); 
    } 

    public boolean bothCombatantsAlive() { 
     return enemyHP > 0 && yourHp > 0; 
    } 

    public void defend() { 
     Random random = new Random(); 
     int randomness = random.nextInt(11) + 10; 
     yourHp = yourHp - randomness; 
     System.out.println("You now have " + yourHp + "HP left!"); 

    } 
} 

class Weapons { 
    public int sword() { 
     return 5 + (int) (Math.random() * ((10 - 5) + 1)); 
    } 

    public int axe() { 
     return 5 + (int) (Math.random() * ((10 - 5) + 1)); 
    } 
} 
+0

ありがとう、私はそれを試してみましょう! –

+0

防衛のために働いていましたが、攻撃のためにそれはまだ再計算を続けています –

+0

あなたの変数は毎回再作成される武器で宣言されています..私はあなたのコードを屈折させる方法を示すために私の答えを編集しました。 – alexbt

関連する問題