//******************************************************************** // Guess a number randomly generated by the program // Demonstrates the use of dialog boxes //******************************************************************** import javax.swing.JOptionPane; import java.util.Random; public class Guess { public static void main (String[] args) { int num, guess, count, option; String msg, clue; Random gen = new Random(); do { num = gen.nextInt(100) + 1; // generate a random number in [1,100] count = 0; do { msg = JOptionPane.showInputDialog ("Enter an integer [1, 100]: "); if (msg != null) { guess = Integer.parseInt(msg); count++; if (guess > num) clue = "The number is smaller"; else if (guess < num) clue = "The number is bigger"; else clue = "The number is " + num + ". You guessed with " + count + " attempts"; JOptionPane.showMessageDialog (null, clue); } else guess = num; } while (guess != num); option = JOptionPane.showConfirmDialog (null, "Play again?"); } while (option == JOptionPane.YES_OPTION); } }