so, for my Java class I need to make a HiLo game. I figured the game out on my own, but I wanted to take it a step further, and after the user completes the game(guesses the right number) I want to ask them if they would like to play again. The problem is, I can't seem to figure out exactly how. I have tried several things and at the moment I'm trying to get them to enter either yes or no to continue playing the game. If someone could help me figure out what I'm doing wrong and explain it to me, that would be great. Thanks!
Random generator = new Random();
Scanner scan = new Scanner(System.in);
int answer, guess;
answer = generator.nextInt(101);
System.out.println("Lets play a game. Guess the number(0-100): ");
guess = scan.nextInt();
while (guess != answer){
System.out.println("Wrong guess.");
if (guess > answer){
System.out.println("Your guess was higher than the answer. Guess again: ");
}
else{
System.out.println("Your guess was lower than the answer. Guess again: ");
}
guess = scan.nextInt();
}
if (guess == answer)
System.out.println("You got it! The number was " + answer);
//after initial game finishes, we ask if they want to play again.
System.out.print("Want to play again? ");
String again = scan.nextLine();
while (again != "no"){
System.out.println();
System.out.print("Great! lets play again.");
System.out.println("Take a guess(0-100 inclusive): ");
guess = scan.nextInt();
while (guess != answer){
System.out.println("Wrong guess.");
if (guess > answer){
System.out.println("Your guess was higher than the answer. Guess again: ");
}
else{
System.out.println("Your guess was lower than the answer. Guess again: ");
}
guess = scan.nextInt();
if (guess == answer)
System.out.println("Your got it! The number was " + answer);
System.out.println("Want to play again? (0 for no, 1 for yes): ");
again = scan.nextLine();
}
}
System.out.println("Thanks for playing!");
}
}