Я работаю над небольшой игрой Rock Paper Scissors с Java, где условие выигрыша или проигрыша основано на том, выиграет ли компьютер или игрок пять игр. Я не уверен, как заставить программу зацикливаться на входе пользователя, если есть неправильный ввод.
Вот код, где у меня эта проблема
Раздел, который я пытаюсь выполнить, - это часть с "else if (definition.equals(" Y ")) {"
import java.util.*;
public class Rock_Paper_Scissors {
public static void main(String arg[]) {
boolean loopGameStart = true;
while (loopGameStart) {
System.out.println("Welcome to Rock Paper Scissors, a game programmed "
+ "by Daniel Park. Would you like to start? (Y/N)");
Scanner userInput = new Scanner(System.in);
String determination = userInput.next();
if (determination.equals("N")) {
System.out.println("Please, do reconsider...");
loopGameStart = true;
} else if (determination.equals("Y")) {
Random rand = new Random();
int n = rand.nextInt(3) + 1;
// 1 = Rock, 2 = Paper, 3= Scissor
int humanWinCount = humanWinCount();
int computerWinCount = computerWinCount();
System.out.println("Choose 0, 1, or 2 (Rock/Paper/Scissor)");
Scanner userRPS = new Scanner(System.in);
int choiceRPS = userRPS.nextInt();
while ((humanWinCount < 5) && (computerWinCount < 5)) {
if (choiceRPS == 0) {
if (n == 1) {
System.out.println("TIE!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 2) {
System.out.println("LOSS!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
computerWinCount = computerWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 3) {
System.out.println("WIN!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
humanWinCount = humanWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else {
System.out.println("I do not understand... Try Again.");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
}
} else if (choiceRPS == 1) {
if (n == 1) {
System.out.println("WIN!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
humanWinCount = humanWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 2) {
System.out.println("TIE!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 3) {
System.out.println("LOSS!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
computerWinCount = computerWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else {
System.out.println("I do not understand... Try again.");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
}
} else if (choiceRPS == 2) {
if (n == 1) {
System.out.println("LOSS");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
computerWinCount = computerWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 2) {
System.out.println("WIN!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
humanWinCount = humanWinCount + 1;
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else if (n == 3) {
System.out.println("TIE!!");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
} else {
System.out.println("I do not understand... Try again.");
System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
choiceRPS = userRPS.nextInt();
}
}
}
if (humanWinCount == 5) {
System.out.println("Congratulations, you win!!");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
System.out.println("Would you like to try again? (Y/N)");
Scanner continueOrNot = new Scanner(System.in);
String contOrNot = continueOrNot.next();
if (contOrNot.equals("Y")) {
loopGameStart = true;
} else if (contOrNot.equals("N")) {
System.out.println("Okay, goodbye!!");
loopGameStart = false;
}
}
if (computerWinCount == 5) {
System.out.println("Boohoo, you lost!!");
System.out.println("Computer: " + computerWinCount + " rounds won");
System.out.println("You: " + humanWinCount + " rounds won");
System.out.println("Would you like to try again? (Y/N)");
Scanner continueOrNot = new Scanner(System.in);
String contOrNot = continueOrNot.next();
if (contOrNot.equals("Y")) {
loopGameStart = true;
} else if (contOrNot.equals("N")) {
System.out.println("Okay, goodbye!!");
loopGameStart = false;
}
}
} else {
System.out.println("I do not understand, please try again!");
}
}
}
public static int humanWinCount() {
int x = 0;
return x;
}
public static int computerWinCount() {
int c = 0;
return c;
}
}