I tried making a number guessing game from http://www.abgamerguys.com/tutorials/java-for-beginners-tutorial-day-3/ and I decided to add my own replay feature.
//Replay feature
boolean replay = true;
while (replay == true) {
System.out.print("Do you want to play again? (y/n) : ");
String askReplay = inputScanner.nextLine();
if (askReplay.equalsIgnoreCase("y")) {
playNumberGame = true;
replay = false;
} else if (askReplay.equalsIgnoreCase("n")) {
playNumberGame = false;
replay = false;
} else {
System.out.println("Unknown reply.");
}
}
The problem is the final else statement runs before i give an input (y/n) and it loops again and then I am able to give an input and runs like it's supposed to.
This is the output I get:
Do you want to play again? (y/n) : Unknown reply.
Do you want to play again? (y/n) :
Full code here.