Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As @SotiriosDelimanolis wrote, you never reassign userGuessDifference. This should be done inside the while loop.</p> <p>Moreover, there is another problem with your code: if you guess the number, the program just prints "CONGRATS YOU WIN!" forever, but it seems to me that you wanted to quit from the while loop once the user guesses the number (I guess the flag variable was introduced for this reason). </p> <p>I slightly changed your code in order to meet this requirement:</p> <pre><code>import java.util.Random; import java.util.Scanner; public class GuessingGame { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); Random generator = new Random(); int difficulty = 0; int guesses = 0; int userGuess = 0; int correctAnswer = 0; int counter = 0; System.out.println("We are going to play a number guessing game."); System.out.println(" "); System.out.println("Choose your difficulty:"); System.out.println("Pick a number - 10 is easy, 25 is medium, 50 is hard."); difficulty = keyboard.nextInt(); if (difficulty == 10) { guesses = 3; System.out.println("You have 3 guesses, make them count!"); } else if (difficulty == 25) { guesses = 5; System.out.println("You have 5 guesses, make them count!"); } else if (difficulty == 50) { guesses = 6; System.out.println("You have 6 guesses, make them count!"); } else { System.out.println("If you can't follow instructions, I'm going to make this very difficult for you!"); difficulty = (difficulty * 100); guesses = 1; } System.out.println(" "); System.out.println("Ok, I have my number. Time to play."); correctAnswer = generator.nextInt(difficulty) + 1; System.out.println("Pick a whole number between 1 and " + difficulty + ":"); userGuess = keyboard.nextInt(); while (counter &lt;= guesses) { // int userGuessDifference = (Math.abs(correctAnswer) - Math // .abs(userGuess)); int userGuessDifference = Math.abs(correctAnswer - userGuess); if (userGuess == correctAnswer) { System.out.println("CONGRATS YOU WIN!"); break; } else if ((userGuessDifference &lt;= (difficulty * .10))) { System.out.println("HOT!"); } else if ((userGuessDifference &lt; (difficulty * .25)) &amp;&amp; (userGuessDifference &gt; (difficulty * .10))) { System.out.println("Warm..."); } else { System.out.println("Ice cold."); } userGuess = keyboard.nextInt(); counter++; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload