I'm creating an application that basically works as a guessing game. At the end of the game I want to ask the user if they want to play again. If they type "Y" or "y", the game will restart, if any other character is entered, then the loop ends.
public class NumberGame {
public static void main(String[] args) throws java.io.IOException {
int NumberGame;
int NumberUser;
char BonusResponse;
char charGen;
String Guess = " ";
String Bonus = " ";
do {
NumberGame = (int) (Math.random() * 10);
charGen = (char)(NumberGame + 48);
//for(NumberGame < 1; NumberGame++){
System.out.print("The computer generated number was " + NumberGame);
System.out.print("\nGuess a number between 1 and 9: ");
NumberUser = (char) System.in.read();
NumberUser = (int) (NumberUser - 48);
if (NumberGame > NumberUser)
Guess = " Too low! Try again";
else if (NumberGame == NumberUser)
Guess = " Congratulations, you win!";
else if (NumberGame < NumberUser)
Guess = " Too high! Try again";
System.out.println("You guessed " + NumberUser + "." + Guess);
if (NumberGame == NumberUser)
Bonus = "Now that you've won, wanna play again? Type Y to play again or any other key to exit:";
else
Bonus = " ";
System.out.println(Bonus);
BonusResponse = (char) System.in.read();
} while (BonusResponse == 'Y' || BonusResponse == 'y');
}
}