2016-09-22 2 views
-5

シンプルなロックペーパーはさみコードを作っていますが、私のprintlnのほとんどは動作していません。私はそれが選択とランドのための私の変数の割り当てだったかどうかを見るために別のものを追加しようとしましたが、何も出力しません。どんな助けでも大歓迎です!System.out.Printlnはスキップされています

import java.util.Scanner; 

public class project4 { 

    public static void main(String[] args) { 
    Random in = new Random(); 
    Scanner key = new Scanner(System.in); 
    System.out.println("Please select one of [R/P/S]: "); 
    String choice = key.nextLine(); 
    int rand = in.nextInt(3) + 1; 
    int choose = 0; 
    if (choice.equals("r") || choice.equals("R")) { 
     choose = 1; 
     System.out.println("You chose Rock"); 
    } else { 
     if (choice.equals("P") || choice.equals("p")) { 
     choose = 2; 
     System.out.println("You chose Paper"); 
     } else { 
     if (choice.equals("s") || choice.equals("S")) { 
      choose = 3; 
      System.out.println("You chose Scissors"); 
     } 
     } 
     System.out.println("rand= " + rand + "choose =" + choose); 
     System.out.flush(); 
    } 
    if (rand == 1) { 
     System.out.println("I chose Rock"); 
    } else { 
     if (rand == 2) { 
     System.out.println("I chose Paper"); 
     } else { 
     System.out.println("I chose Scissors"); 
     } 

     if (choose == rand) { 
     System.out.println("Its a Tie!"); 
     } else { 
     if (choose == 1 && rand == 2) { 
      System.out.println("Paper beats Rock, You lose!"); 
     } else { 
      if (choose == 1 && rand == 3) { 
      System.out.println("Rock beats Scissors, You win!"); 
      } else { 
      if (choose == 2 && rand == 1) { 
       System.out.println("Paper beats Rock, You win!"); 
      } else { 
       if (choose == 2 && rand == 3) { 
       System.out.println("Scissors beats Paper, You lose!"); 
       } else { 
       if (choose == 3 && rand == 1) { 
        System.out.println("Rock beats Scissors, You lose!"); 
       } else { 
        System.out.println("Scissors beats Paper, You win!"); 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 
+2

最初のステップは、オールウェイズ、習慣実行するJavaコードを疑う開始ドントべきです。ほとんどの場合、コードをスキップするのではなく、自分のロジックを疑うでしょう。また、何も印刷されませんか?私にとってそれは – SomeJavaGuy

+0

のどちらかを選択した後、それが選択したことを言う前に、変数randと値を持つものを出力する必要があります。それからあなたが勝つと言っているか、またはあなたが負うと言うラインのいずれか、またはそのネクタイ!日食で私のために出力されていません。 –

+0

これをきれいにフォーマットしないために、* way *がたくさんあります。それを修正してください(-1)。 – ChiefTwoPencils

答えて

0

あなたのSystem.out.printlnあなたが「他の」条件内でそれらのいくつかを入れているので、スキップされています。このため、 'IF'条件が真の場合、elseブロックはスキップされ、sysoutもスキップされます。以下のコードはうまくいくはずです!

また、ネストされたif条件の代わりにelse-ifを使用することをお勧めします。これは、読みやすく、エラーが発生しにくいからです。

package project4; 
import java.util.Random; 
import java.util.Scanner; 
public class project4 { 

public static void main(String[] args) { 
    Random in = new Random(); 
    Scanner key = new Scanner(System.in); 
    System.out.println("Please select one of [R/P/S]: "); 
    String choice = key.nextLine(); 
    int rand = in.nextInt(3) + 1; 
    int choose = 0; 
    if (choice.equals("r") || choice.equals("R")) { 
     choose = 1; 
     System.out.println("You chose Rock"); 
    } else { 
     if (choice.equals("P") || choice.equals("p")) { 
      choose = 2; 
      System.out.println("You chose Paper"); 
     } else { 
      if (choice.equals("s") || choice.equals("S")) { 
       choose = 3; 
       System.out.println("You chose Scissors"); 
      } 
     } 
    } 
    System.out.println("rand= " + rand + "choose =" + choose); 
    System.out.flush(); 
    if (rand == 1) { 
     System.out.println("I chose Rock"); 
    } else { 
     if (rand == 2) { 
      System.out.println("I chose Paper"); 
     } else { 
      System.out.println("I chose Scissors"); 
     } 
    } 

    if (choose == rand) { 
     System.out.println("Its a Tie!"); 
    } else { 
     if (choose == 1 && rand == 2) { 
      System.out.println("Paper beats Rock, You lose!"); 
     } else { 
      if (choose == 1 && rand == 3) { 
       System.out.println("Rock beats Scissors, You win!"); 
      } else { 
       if (choose == 2 && rand == 1) { 
        System.out.println("Paper beats Rock, You win!"); 
       } else { 
        if (choose == 2 && rand == 3) { 
         System.out.println("Scissors beats Paper, You lose!"); 
        } else { 
         if (choose == 3 && rand == 1) { 
          System.out.println("Rock beats Scissors, You lose!"); 
         } else { 
          System.out.println("Scissors beats Paper, You win!"); 
         } 
        } 
       } 
      } 
     } 
    } 
} 

}

関連する問題