Glitches
boolean correctGuess = false; for(int i = 0; i < 10 && !correctGuess; i++) { System.out.println("Enter a number in the range of: " + minNumber + " to " + maxNumber + "You have up to 10 guesses."); int userValue = In.getInt(); if (userValue == randomNumber) correctGuess = true; if (userValue > randomNumber && userValue <= maxNumber) { System.out.println("Your guess is too high."); } else if (userValue < randomNumber && userValue >= minNumber) { System.out.println("Your guess is too low."); } else if (userValue >maxNumber || userValue < minNumber) { System.out.println("Invalid guess, guess a number within the specified range."); } else if (correctGuess) { System.out.println("Congratulations! You guessed the number correctly!You have won the game!"); }
So when run this, it works fine when the user guesses the correct answer. But when the incorrect answer is given, it does not print "Sorry you've lost etc" I had that statement else right after the code above, but it just prints, "too low/too high " and exits after 10 guesses. Any suggestions for fixing this?
Well, this block does not totain the "Lost" message. You want another iff after this to deal with the losing case, or do a different loop. ``` boolean correctGuess = false; for (int i = 0; i < 10 && !correctGuess; i++) { System.out.println("Enter a number in the range of: " + minNumber + " to " + maxNumber + "You have up to 10 guesses."); int userValue = In.getInt(); if (userValue == randomNumber) correctGuess = true; if (userValue > randomNumber && userValue <= maxNumber) { System.out.println("Your guess is too high."); } else if (userValue < randomNumber && userValue >= minNumber) { System.out.println("Your guess is too low."); } else if (userValue > maxNumber || userValue < minNumber) { System.out.println("Invalid guess, guess a number within the specified range."); } else if (correctGuess) { System.out.println("Congratulations! You guessed the number correctly!You have won the game!"); } } if (correctGuess == false) System.out.println("Sorry, you lost."); ``` Or here are some edits on the fly (sorry if I make mistakes) ``` guesses = 10 System.out.println("Enter a number in the range of: " + minNumber + " to " + maxNumber + "You have up to 10 guesses."); while (guesses > 0) { if (guesses > 1) System.out.print("10 guesses left. Your guess is? "); else System.out.print("And your last guess is? "); int userValue = In.getInt(); if (userValue == randomNumber) { System.out.println("Congratulations! You guessed the number correctly! You have won the game!"); guesses = -1; } else if (guesses == 1) { System.out.println("Sorry, you have guessed wrong all 10 times. You lose."); break; } else if (userValue > randomNumber && userValue <= maxNumber) { System.out.println("Your guess is too high."); } else if (userValue < randomNumber && userValue >= minNumber) { System.out.println("Your guess is too low."); } else if (userValue > maxNumber || userValue < minNumber) { System.out.println("Invalid guess, guess a number within the specified range."); } guesses--; } ```
Thank you very much!
Join our real-time social learning platform and learn together with your friends!