title: "Guess the number" in Java
body: <p>We are meant to to create a program in java in which the computer randomly guesses a number between 1-100 and allows the user to guess to the number. If the number is lower than the random number the program should say: lower! and of higher, the program should say: higher! If the user guesses the right number it should say congratulations you guessed the right number in X amount of tries, This is what I have so far, when I execute in cmd it just spams either higher or lower and I need help working it out.</p>
  
  <pre><code>import java.util.Scanner;
  import java.util.Random;
  
  public class GuessingGame{
  
      public static void main(String[] args) {
  
          int random, guess, attempts;
          Scanner keyboard = new Scanner(System.in);
          Random generator = new Random();
          random = generator.nextInt(100) + 1;
          attempts = 1; 
  
          System.out.print("I am thinking of a number between 0 and 100, what do you think it is?");
  
          guess = keyboard.nextInt(); 
          while (guess != random) {
              if (guess &gt; random) {
                  System.out.print("Lower!");
                  attempts += 1; 
              }
              else {
                  System.out.print("Higher!");
                  attempts +=1;
              } 
          }
  
          System.out.print(random + "is the correct answer and it took you" + attempts + "attempts to guess it!");
  
      }        
  }
  </code></pre>