
is there a way to declare a char early on but use it only later, because when i try to declare it as 0 early on then it will just cause an error because the answer of while is supposed to be 'Y'. i could get it to loop and play again by asking the question before playing but I only want it to ask the option to play again at the end of the game. would appreciate it if anyone could tell me how to get this to work, thank you.
public class soodsami_a3 {
public static void main(String[] args) {
Scanner sam = new Scanner(System.in);
// Random number generator
int Randomizer = (int)(Math.random() * 100) + 1;
while (playagain == 'y') {
System.out.println("I'm thinking of a number between 1 and 100");
System.out.println("What is it?");
System.out.print("Guess: ");
int Useranswer = sam.nextInt();
while (Useranswer != Randomizer) {
if (Useranswer < Randomizer) {
System.out.println("Too low.");
System.out.print("Guess: ");
Useranswer = sam.nextInt();
} else if (Useranswer > Randomizer) {
System.out.println("Too high.");
System.out.print("Guess: ");
Useranswer = sam.nextInt();
}
if (Useranswer == Randomizer) {
System.out.println("You got it!");
}
System.out.print("would you like to play again (Y/N) ");
char playagain = sam.next().toUpperCase().charAt(0);
}
}
System.out.println("Thanks for playing");
}
}