import java.util.Random;
import javax.swing.JOptionPane;

/** A high-low guessing game. */
public class HighLowGUI2 {
 
  /**The number to guess. */
  private int number;
  
  /** 
   * Create a new high-low game with a number
   * to guess between 0 and upperbound. 
   */ 
  public HighLowGUI2(int upperbound) {
    
    // a random number generator
    Random r = new Random();

    // set the number to guess to a random integer
    // between 0 and upperbound
    this.number = r.nextInt(upperbound);
  }
  
  /**
   * Guess the number.
   */
  public void makeGuess() {
    
    String inp = JOptionPane.showInputDialog("Enter a number:");
    int guess = Integer.parseInt(inp);
    
    // repeat asking user until the guess is correct
    while (guess != number){ 
      if (guess < this.number) {
        JOptionPane.showMessageDialog(null, "Too low!");
      } else {
        JOptionPane.showMessageDialog(null, "Too high!");
      }
      
      // note: the following 2 lines are important, to ask for next number
      // otherwise, keep looping forever without any change!
      inp = JOptionPane.showInputDialog("Make another guess:");
      guess = Integer.parseInt(inp);   
    }
    
    // you can only exit the loop if you guessed the number...
    JOptionPane.showMessageDialog(null, "You got it!");
  }
}