The code below is part of a number guessing game, in which the computer generates a random number within a user-specified range. Here, I'm trying to limit the user to 10 guesses. If he/she exceeds 10, then the game ends:
int t1 = 0;//stores # of guesses
while(true){//loop begins
t1++;//increments each iteration to represent # of guesses
if(g1!=n){//if the guess is incorrect... (n = # user is trying to guess)
if(t1>10){//# of guesses cannot exceed 10
System.out.println("\nGAME OVER\nYou have exceeded the max # of tries!");
break;}//game ends if user exceeds max # of attempts
if(g1<n){System.out.println("\nGuess Higher!\n" + t1 + "attempt(s) so far");
continue;}//guess is too low
if(g1>n){System.out.println("\nGuess Lower!\n" + t1 + " attempt(s) so far");
continue;}}//guess is too high (loop ends)
The output I recieve is only 1/2 correct. For example, assume the computer has generated the number 12 in a range of 1-100. On the user's 10th (last) guess, it will print: "GAME OVER..."; however, it will also print if the user's guess is too low or tooh igh, which I don't want.
What change do I make to correct this error? I think it has to do with the 'break' statement in the nested-if.
for loopor awhile (t1++ < 10)loop