2017-10-05 14 views
-3

私はロックペーパーのハサミプログラムをJavaで作っていますし、すべてのコードを持っています。それは私に必要な出力を与えるはずですが、実行するたびに入力ステートメントだけを出力します。私はdo文の後に入力文がある行を印刷し、それを出力しますが、if文には何も印刷されません。理由は分かりません。これは私が持っているものです。助けていただきありがとうございます。要求コードを移動:デバッグを行い、より簡単にプログラムを読み込むために入力後にプログラムが印刷されないのはなぜですか?

public static void main(String[] args) 
{ 
    Scanner input = new Scanner(System.in); 
    Random rand = new Random(); 

    String replay = (""); 
    String user = (""); 
    int ai = 0; 
    String aiPlay = (""); 

    do 
    { 
     ai = rand.nextInt(3) + 1; 

     switch (ai) //was if statements but java suggested to change to switch statements 
     { 
      case 1: 
       aiPlay = ("Rock"); 
       break; 
      case 2: 
       aiPlay = ("Paper"); 
       break; 
      default: 
       aiPlay = ("Scissors"); 
       break; 
     } 

     do //make sure input is correct 
     { 
      System.out.print("rock, paper, or scissors: "); 
      user = input.next(); 
     }while(!(user.equals("rock") || user.equals("paper") || user.equals("scissors"))); 

     if(user.equals(ai)) 
     { 
     System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nIt was a tie!"); 
     System.out.print("Continue? (y or n): "); 
     replay = input.next(); 
     } 
     switch (user) { //was if statements java suggested it change to switch statement 
      case "rock": 
       if (aiPlay.equals("scissors")) 
       { 
        System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou win!"); 
        System.out.print("Continue? (y or n): "); 
        replay = input.next(); 
       } 

       else if (aiPlay.equals("paper")) 
       { 
        System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou lose!"); 
        System.out.print("Continue? (y or n): "); 
        replay = input.next(); 
       } break; 
      case "paper": 
       if (aiPlay.equals("scissors")) 
       { 
        System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou win!"); 
        System.out.print("Continue? (y or n): "); 
        replay = input.next(); 
       } 

       else if (aiPlay.equals("rock")) 
       { 
        System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou lose!"); 
        System.out.print("Continue? (y or n): "); 
        replay = input.next(); 
       } break; 
      case "scissors": 
       if (aiPlay.equals("paper")) 
       { 
        System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou win!"); 
        System.out.print("Continue? (y or n): "); 
        replay = input.next(); 
       } 

       else if (aiPlay.equals("rock")) 
       { 
        System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou lose!"); 
        System.out.print("Continue? (y or n): "); 
        replay = input.next(); 
       } break; 
      default: 
       break; 
     } 

    }while(replay.equals("y")); 
} 
+0

https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – David

+0

ヒントそれは常に同じであるので、switch文からのリプレイ(ループの最後まで)。また、これらの行を見てみましょう: 'aiPlay =(" Rock ")'と 'aiPlay.equals(" rock ")' - 問題がありますか? – Thomas

+1

AIは "ロック"、 "紙"または "ハサミ"を行い、ユーザは "ロック"、 "紙"または "はさみ"を行います。それらの選択肢のいずれかが等しいか? –

答えて

0
switch (ai) //was if statements but java suggested to change to switch 
statements 
    { 
     case 1: 
      aiPlay = ("rock"); /* All should be lowercase*/ 
      break; 
     case 2: 
      aiPlay = ("paper"); /* All should be lowercase*/ 
      break; 
     default: 
      aiPlay = ("scissors"); /* All should be lowercase*/ 
      break; 
    } 
関連する問題