hemlet

PickNumber

Feb 2nd, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2. import java.util.Random;
  3.  
  4. public class PickNumber
  5. {
  6.     static final int TRY = 3;
  7.     public static void main(String[] args)
  8.     {
  9.         Random generator = new Random();
  10.        
  11.         String[] names = {"Shirley","Ishmael","Patricia","Barbara","Daisy","Maude","Prionsios", "Leonard","Nero","Judith","Mavis","Millie","Rufus"};
  12.         String nameEnt;
  13.         String inputString;
  14.         String outputString;
  15.         boolean isGood = false;
  16.         int counter = 0;
  17.         int r = generator.nextInt(names.length); // random number used to pick name from names array
  18.         int num = generator.nextInt(5)+1;// generate random int 0-4 inc (length 5) + 1 to be between 1 and 5
  19.         int guess;
  20.        
  21.         nameEnt = JOptionPane.showInputDialog("Name ");
  22.        
  23.         //do while enter guess
  24.         do
  25.         {
  26.             counter++;
  27.             inputString = JOptionPane.showInputDialog("Pick a number between 1 and 5: (" + counter +"/" + TRY +") ");
  28.             guess = Integer.parseInt(inputString);
  29.             if ( num == guess)
  30.             {
  31.                 isGood = true;
  32.             }
  33.            
  34.         } while (counter < TRY && !(isGood));//end do while
  35.        
  36.        
  37.         if (isGood)
  38.         {
  39.             outputString = "Congrats " + nameEnt + " your guess " + guess + "\nis correct!" + "\nPoints: " + (1 + TRY - counter);
  40.             JOptionPane.showMessageDialog(null,outputString, "Correct " + nameEnt,JOptionPane.INFORMATION_MESSAGE);
  41.         } else
  42.         {
  43.             outputString = "Do not pass Go! " + guess + " is not correct " + "\nIt is, in fact, " + num + "." +
  44.                 "\nToday I will call you " + names[r] + ".";
  45.             JOptionPane.showMessageDialog(null,outputString, "Idiot",JOptionPane.WARNING_MESSAGE);
  46.         }
  47.        
  48.         System.exit(0);
  49.        
  50.     }//main
  51. }//class
Advertisement
Add Comment
Please, Sign In to add comment