2016-10-24 8 views
-3

コーディングにはまったく新しいですが、私のコードでこの問題を見つけるのには苦労しています。それは他のいくつかのクラスを実行し、あなたが再びプレイしたいかどうか尋ねるクラスです。while文の間違った値java

問題は、最初のゲームを選択してから、もう一度再生したいと答えた後に2番目のゲームをプレイするときにnまたはいいえを再生すると、代わりに再生するゲームゲームを繰り返す。

public static void arcade() { 
    Scanner scanner2 = new Scanner(System.in); 
    String choice; 
    int game; 
    boolean finish; 
    finish = false; 
    game = 0; 

    do { 
     try { 

      System.out.println(
        "Which game would you like to play? \n1. Coin toss\n2. Rock Paper Scissors \n3. Number Game\n4. Exit"); 
      game = scanner2.nextInt(); 

      switch (game) { 
      case 1: 
       choice = "yes"; 
       do { 
        if (choice.equalsIgnoreCase("yes") || choice.equalsIgnoreCase("y")) { 
         coinToss(scanner2); 
         System.out.println("Would you like to play again?"); 
         choice = scanner2.next(); 
        } else if (choice.equalsIgnoreCase("no") || choice.equalsIgnoreCase("n")) { 
         System.out.println("Goodbye"); 
         finish = true; 
        } else { 
         System.out.println("Invalid selection"); 
         choice = scanner2.next(); 

        } 
       } while (finish != true); 
       break; 
      case 2: 
       choice = "yes"; 
       do { 
        if (choice.equalsIgnoreCase("yes") || choice.equalsIgnoreCase("y")) { 
         rockPaperScissors(scanner2); 
         System.out.println("Would you like to play again?"); 
         choice = scanner2.next(); 
        } else if (choice.equalsIgnoreCase("no") || choice.equalsIgnoreCase("n")) { 
         System.out.println("Goodbye"); 
         finish = true; 

        } else { 
         System.out.println("Invalid selection"); 

        } 
       } while (finish != true); 
       break; 
      case 3: 
       choice = "yes"; 
       do { 
        if (choice.equalsIgnoreCase("yes") || choice.equalsIgnoreCase("y")) { 
         numberGame(scanner2); 
         System.out.println("Would you like to play again?"); 
        } else if (choice.equalsIgnoreCase("no") || choice.equalsIgnoreCase("n")) { 
         System.out.println("Goodbye"); 
         finish = true; 
        } else { 
         System.out.println("Invalid selection"); 

        } 
       } while (finish != true); 
       break; 
      case 4: 
       System.out.println("Goodbye"); 
       break; 
      default: 
       System.out.println("Invalid selection"); 
      } 

     } catch (java.util.InputMismatchException e) { 
      System.err.println("Please use numbers"); 
      scanner2.nextLine(); 
     } 

    } while (game != 4); 
    scanner2.close(); 
} 
+0

応じてコードと[編集]あなたのポスト投函で[MCVE]ガイダンスをお読みください。 –

+0

最後に質問を削除したのはなぜですか? –

答えて

0

あなたは再初期化するたびにfalseにfinish変数忘れて:あなたはすべてのケースのためにそれを行う必要があります

case 2: 
     choice = "yes"; 
     do { 
      finish = false; 
      ... 

を。また、3番目のスイッチでは、"Would you like to play again?"を入力した後に入力をスキャンするのを忘れたことも考慮してください。

最後に、あなたのコードビットをリファクタリングしてみてください:)

+0

ちょっと助けてくれてありがとうございました。 – CHurst

関連する問題