我的RPS程序很好。我还在尝试了解while循环,我不知道如何处理我的游戏。一切正常,直到我对#34;是或否"当它问我是否想再玩一次。如果我说是的话,它会重新开始游戏,但是当我在之前的无效答案之后被提示再次尝试时我说不,它会再次播放,好像我说的是。我对此很陌生,因为生病而没有上过课。我该如何解决这个问题?我希望它能够回到原来的结果#34;是"和"不"第一个剪辑是我的问题,第二个是代码本身:
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 :(得分:0)
您的代码存在问题:
else while (!response.equalsIgnoreCase("yes") && !response.equalsIgnoreCase("no")) {
System.out.println("Error! Invalid response. Try again:");
response = scan.next();
}
一旦第一个回复不是"是"或者" no",你最终会进入这个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"));