Okay, so here is my code:
public static void playAgain(Scanner in){
System.out.print("Play again?: ");
String again=in.next();
if (again.equalsIgnoreCase("y")){
playerScore=0;
aiScore=0;
playAgain=true;
}
else if (again.equalsIgnoreCase("n")){
playAgain=false;
}
else {
while (!again.equalsIgnoreCase("y") && !again.equalsIgnoreCase("n")){
System.out.print("Invalid response. Please enter \"y\" or \"n\": ");
again=in.next();
}
}
}
For some reason, if I input the wrong variable, say 'boog', the while loop prints an error message but for some reason defaults to 'y' even if I input 'n' - for example, a sample run would be:
Play again? boog
Invalid input, please input y or n. n
-program plays again despite my inputting n-
How do I fix this? Is it something with the order? Thanks in advance!
Invalid response, but your screen protocol saysInvalid input. That doesn't match. Besides that, I would make the methodreturn playAgaindirectly, instead of saving it into a field variable. The fewer fields you have in a class, the easierr it is to understand.