HI, I\'m writing a Java Program for Rock, Paper, Scissors (Jan Ken Po), i have t
ID: 3753113 • Letter: H
Question
HI,
I'm writing a Java Program for Rock, Paper, Scissors (Jan Ken Po), i have the program working for all the possibliteis. The problem i am having is if unepxected entry is made, the progrm will print out the Invalid input, please try again and the computer selction. Can you help me stop the computer slection from priting upon a invalid entry.
public static void main(String[] args) {
while (true) {
String playerChoice = "";
String computerChoice = " ";
String response;
int computerInt;
int compScore = 0;
int playerScore = 0;
int rounds = 0;
Scanner scan = new Scanner(System.in);
Random generator = new Random();
while (playerScore < 3) {
{
System.out.println(" How about a game of Jan Ken Po!");
System.out.println("Please make your choice. " + "Rock "
+ " Paper" + " or Scissors");
System.out.println();
{
computerInt = generator.nextInt(3) + 1;
//computer choices
if (computerInt == 1) {
computerChoice = "Rock";
} else if (computerInt == 2) {
computerChoice = "Paper";
} else if (computerInt == 3) {
computerChoice = "Scissors";
}
//player choice input
System.out.println(" Make your play: ");
playerChoice = scan.next();
while (!playerChoice.equalsIgnoreCase("Rock") && !playerChoice.equalsIgnoreCase("Paper")
&& !playerChoice.equalsIgnoreCase("Scissors")) {
System.out.println("Invalid user input, please try again.");
break;
}
System.out.println("My play is: " + computerChoice); //this prints when a invalid entry is made and i can't figure out how to make it stop yet still have it print as part of the valid game.
//deciding who wins
if (playerChoice.equalsIgnoreCase(computerChoice)) {
playerScore++;
compScore++;
System.out.println("Aiko Desho");
} else if (playerChoice.equalsIgnoreCase("Rock")) {
if (computerChoice.equals("Scissors")) {
playerScore++;
System.out.println("Rock crushes scissors. You win!!");
} else if (computerChoice.equals("Paper")) {
compScore++;
System.out.println("Paper eats rock. You lose!!");
}
} else if (playerChoice.equalsIgnoreCase("Paper")) {
if (computerChoice.equals("Scissors")) {
System.out.println("Scissor cuts paper. You lose!!");
compScore++;
} else if (computerChoice.equals("Rock")) {
playerScore++;
System.out.println("Paper eats rock. You win!!");
}
} else if (playerChoice.equalsIgnoreCase("Scissors")) {
if (computerChoice.equals("Paper")) {
System.out.println("Scissor cuts paper. You win!!");
playerScore++;
} else if (computerChoice.equals("Rock")) {
compScore++;
System.out.println("Rock breaks scissors. You lose!!");
}
} else {
}
System.out.println(" Play again? "Yes" or "No"");
Scanner YesNo = new Scanner(System.in);
String YesNo_String = YesNo.next();
if (YesNo_String.equalsIgnoreCase("Yes")) {
}
if (YesNo_String.equalsIgnoreCase("No")) {
System.out.println("Goodbye!");
return;
}
{
//print out scores
System.out.println(" Player " + playerScore);
System.out.println("computer " + compScore);
}
break;
}
}
}
}
}
}
Explanation / Answer
The best way to prevent this is to take input till the entered input is correct.
So modify your error part to this :-
System.out.println(" Make your play: ");
playerChoice = scan.next();
while (!playerChoice.equalsIgnoreCase("Rock") && !playerChoice.equalsIgnoreCase("Paper")
&& !playerChoice.equalsIgnoreCase("Scissors")) {
System.out.println("Invalid user input, please try again.");
// Take input again
System.out.println(" Make your play: ");
playerChoice = scan.next();
// while loop so that the next input is checked again
}
// This will prevent running further when theoutput is invalid.
Rate positive if satisfied!! :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.