import javax.swing.*; /** A game for guessing a number. */ public class GuessingGame { /** The number to be guessed. */ private int solution; /** * A GuessingGame with solution n. * @param n the number to be guessed. */ public GuessingGame(int n) { this.solution= n; } /** * Play the guessing game. Continually prompt the user for a guess until * they guess the solution. Let them know whether their guesses are too * high or too low. */ public void play() { String message= "Guess an integer."; String guess= JOptionPane.showInputDialog(message); while (Integer.parseInt(guess) != solution) { if (Integer.parseInt(guess) > solution) { message= "Your guess was too high. Guess again."; } else { message= "Your guess was too low. Guess again."; } guess= JOptionPane.showInputDialog(message); } JOptionPane.showMessageDialog(null, "You're right!"); } }