2016-10-24 1 views
-3

Ziemlich neu in der Codierung, aber ich habe Probleme, das Problem in meinem Code zu finden. Es ist nur eine Klasse, die ein paar andere Klassen laufen lässt und fragt, ob du wieder spielen willst oder nicht.falscher Wert in while-Anweisung java

Das Problem ist, wenn Sie das erste Spiel wählen und dann n oder nein sagen, um nicht mehr zu spielen, wenn Sie ein zweites Spiel spielen, nachdem Sie geantwortet haben, dass Sie wieder spielen möchten das Spiel wiederholen.

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

Bitte lesen Sie [MCVE] Leitlinien für die Entsendung Code und [Bearbeiten] für Ihren Beitrag entsprechend. –

+0

Warum haben Sie Ihre letzte Frage gelöscht? –

Antwort

0

Ihr Passwort jedes Mal die finish Variable auf false neu initialisieren:

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

Sie es für jeden Fall tun sollten. Bedenken Sie auch, dass Sie in Ihrem 3. Switch vergessen haben, nach der Eingabe "Would you like to play again?" nach der Eingabe zu suchen.

versuchen Schließlich Ihren Code ein wenig Refactoring :)

+0

Danke für die Hilfe einfach stecken und es funktioniert perfekt – CHurst