Hi. I'm stuck on my program. Here's the instruction: design and implement an application that plays the HI-Lo guessing game with numbers. The program should pick a random number between 1 and 100 (inclusive), then keep asking the user to guess the number. On each guess, report to the user that he or she is correct or that the guess is high or low. Keep accepting guesses until the user guesses correctly or quits. Use a sentinel value to determine whether the use wants the quit. Count the number of guesses and report that value when the user guesses correctly. At the end of each game ask whether the user wants to play again. keep playing games until the user chooses to stop.
like my code works the first time, but if I want to play again, it messes up. Then I started playing around with it and it turned into an infinite loop and I can't revert back to my original code.
WARNING: YOULL GET INFINITE LOOP IF YOU RUN MY CODE.
can please someone tell me what's wrong?
import java.util.*;
public class HiLo {
public static void main(String[] args)
{
final int MAX = 100;
int correct = 0, count = 0, sum = 0, sum1 = 0, guess;
String str, another = "y";
Scanner scan = new Scanner (System.in);
Random generator = new Random ();
int answer = generator.nextInt(MAX) + 1;
System.out.println ("Pick a number between 1 and 100: ");
guess = scan.nextInt();
while (another.equalsIgnoreCase("y"))
{
if (guess == answer)
{
count++;
correct++;
System.out.println("\nYou got the right number!!\n");
sum+= count;
sum1+= correct;
System.out.println("Total amount of guesses: " + sum);
System.out.println("\nTotal amount of correct guess: " + sum1);
}
else if (guess > answer)
{
count++;
System.out.println("\nYour guess is high!!\n");
System.out.println ("Pick a number between 1 and 100: (-1 to quit)");
guess = scan.nextInt();
}
else
{
count++;
System.out.println("\nYour guess is low!!\n");
System.out.println ("Pick a number between 1 and 100: (-1 to quit)");
guess = scan.nextInt();
}
}
System.out.println ("Play again? (y/n)?" );
another = scan.nextLine();
another = scan.nextLine();
}
}