2017-04-06 8 views
-1

RPSプログラムはかなりうまく機能しています。私はまだwhileループについて学びたいと思っています。私は自分のゲームを終わらせるために何をすべきか分かりません。もう一度遊びたいかどうか聞いてきたら、「はい、いいえ」と答えたときに無効な答えを出すまで、すべてが機能します。私が「はい」と答えると、ゲームは元の状態に戻りますが、前の無効な回答の後に再試行するよう促された後は、いいえと言います。私はこれが初めてで病気のために授業を受けていません。これをどうやって解決するのだろうか?私は戻って、「はい」の本来の成果にループにそれをしたいと「ノー」最初のスニップは、第二は、コード自体で、私の問題である:RPSゲームを終了しますか?

Would you like to play again? (yes or no) 
nah 
Error! Invalid response. Try again: 
no 
Welcome to the Rock - Paper - Scissors Game! 

Please enter your selection (Rock, Paper, or Scissors): 



Scanner scan = new Scanner(System.in); 
    Random generator = new Random(); 
    String computerChoice; 
    String user; 
    String playerChoice; 
    boolean keepPlaying = true; 
    int playerWins = 0; 
    int playerTies = 0; 
    int playerLosses = 0; 
    int roundTotal = 0; 
    int computer; 
    while (keepPlaying == true) { 
    System.out.println("Welcome to the Rock - Paper - Scissors Game!"); 
    System.out.println(); 
    System.out.println("Please enter your selection (Rock, Paper, or Scissors): "); 
    playerChoice = scan.next(); 
    playerChoice = playerChoice.substring(0,1).toUpperCase()+ playerChoice.substring(1).toLowerCase(); 
    computer = generator.nextInt(3)+1; 
    if(computer == 1) 
    { 
     computerChoice = "Rock"; 
    } 
    else if(computer == 2) 
    { 
     computerChoice = "Paper"; 
    } 
    else if (computer == 3) 
    { 
     computerChoice = "Scissors"; 
    } 
    else 
    { 
     computerChoice = "Rock"; 
    } 

    System.out.println("User: " +playerChoice); 
    System.out.println("Comp: " +computerChoice); 
    if(computer==1 && playerChoice.equalsIgnoreCase("rock")) 
    { 
     System.out.println("Match Outcome -> User Ties"); 
     roundTotal++; 
     playerTies++; 
    } 
    else if(computer==2 && playerChoice.equalsIgnoreCase("paper")) 
    { 
     System.out.println("Match Outcome -> User Ties"); 
     roundTotal++; 
     playerTies++; 
    } 
    else if(computer==3 && playerChoice.equalsIgnoreCase("scissors")) 
    { 
     System.out.println("Match Outcome -> User Ties"); 
     roundTotal++; 
     playerTies++; 
    } 
    else if(computer==1 && playerChoice.equalsIgnoreCase("scissors")) 
    { 
     System.out.println("Match Outcome -> User Loses"); 
     roundTotal++; 
     playerLosses++; 
    } 
    else if(computer==1 && playerChoice.equalsIgnoreCase("paper")) 
    { 
     System.out.println("Match Outcome -> User Wins"); 
     roundTotal++; 
     playerWins++; 
    } 
    else if(computer==2 && playerChoice.equalsIgnoreCase("rock")) 
    { 
     System.out.println("Match Outcome -> User Loses"); 
     roundTotal++; 
     playerLosses++; 
    } 
    else if(computer==2 && playerChoice.equalsIgnoreCase("scissors")) 
    { 
     System.out.println("Match Outcome -> User Wins"); 
     roundTotal++; 
     playerWins++; 
    } 
    else if(computer==3 && playerChoice.equalsIgnoreCase("rock")) 
    { 
     System.out.println("Match Outcome -> User Wins"); 
     roundTotal++; 
     playerWins++; 
    } 
    else if(computer==3 && playerChoice.equalsIgnoreCase("paper")) 
    { 
     System.out.println("Match Outcome -> User Loses"); 
     roundTotal++; 
     playerLosses++; 
    } 
    else { 
     System.out.println("Error! Invalid response."); 
    } 
    String response = ""; 
    System.out.println("Would you like to play again? (yes or no)"); 
    response = scan.next(); 
    if(response.equalsIgnoreCase("no")) 
    { 
     keepPlaying = false; 
     System.out.println("Out of " +roundTotal+ " total rounds, the user had " +playerWins+ " total wins, " +playerLosses+ " total loses, and " +playerTies+ " total ties."); 
     System.out.println(); 
     System.out.println("Thank you for playing!"); 
    } 
    else if(response.equalsIgnoreCase("yes")) 
    { 
     keepPlaying = true; 
    } 
    else while (!response.equalsIgnoreCase("yes") && !response.equalsIgnoreCase("no")) 
    { 
    System.out.println("Error! Invalid response. Try again:"); 
     response = scan.next(); 
    } 
} 

} 
} 

答えて

0

あなたのコードに問題はここにある:

else while (!response.equalsIgnoreCase("yes") && !response.equalsIgnoreCase("no")) { 
    System.out.println("Error! Invalid response. Try again:"); 
    response = scan.next(); 
} 

最初の応答が「はい」か「いいえ」ではありませんしたら、あなたはこのelse句で終わります。ここでは、「はい」または「いいえ」が表示されるまで反応を読みますが、有効な応答が得られれば何もしないので、メインループに戻り、新しいゲームを開始します。

あなたはこのようなあなたのコードの最後の部分を書き換えて、それを修正することができます:

String response = ""; 
System.out.println("Would you like to play again? (yes or no)"); 
do { 
    response = scan.next(); 
    if (response.equalsIgnoreCase("no")) { 
     keepPlaying = false; 
      System.out.println("Out of " + roundTotal + " total rounds, the user had " + playerWins 
         + " total wins, " + playerLosses + " total loses, and " + playerTies + " total ties."); 
     System.out.println(); 
     System.out.println("Thank you for playing!"); 
    } else { 
     if (response.equalsIgnoreCase("yes")) { 
      keepPlaying = true; 
     } else { 
      System.out.println("Error! Invalid response. Try again:"); 
     } 
    } 
} while (!response.equalsIgnoreCase("yes") && !response.equalsIgnoreCase("no")); 
関連する問題