Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- import java.util.Random;
- public class PickNumber
- {
- static final int TRY = 3;
- public static void main(String[] args)
- {
- Random generator = new Random();
- String[] names = {"Shirley","Ishmael","Patricia","Barbara","Daisy","Maude","Prionsios", "Leonard","Nero","Judith","Mavis","Millie","Rufus"};
- String nameEnt;
- String inputString;
- String outputString;
- boolean isGood = false;
- int counter = 0;
- int r = generator.nextInt(names.length); // random number used to pick name from names array
- int num = generator.nextInt(5)+1;// generate random int 0-4 inc (length 5) + 1 to be between 1 and 5
- int guess;
- nameEnt = JOptionPane.showInputDialog("Name ");
- //do while enter guess
- do
- {
- counter++;
- inputString = JOptionPane.showInputDialog("Pick a number between 1 and 5: (" + counter +"/" + TRY +") ");
- guess = Integer.parseInt(inputString);
- if ( num == guess)
- {
- isGood = true;
- }
- } while (counter < TRY && !(isGood));//end do while
- if (isGood)
- {
- outputString = "Congrats " + nameEnt + " your guess " + guess + "\nis correct!" + "\nPoints: " + (1 + TRY - counter);
- JOptionPane.showMessageDialog(null,outputString, "Correct " + nameEnt,JOptionPane.INFORMATION_MESSAGE);
- } else
- {
- outputString = "Do not pass Go! " + guess + " is not correct " + "\nIt is, in fact, " + num + "." +
- "\nToday I will call you " + names[r] + ".";
- JOptionPane.showMessageDialog(null,outputString, "Idiot",JOptionPane.WARNING_MESSAGE);
- }
- System.exit(0);
- }//main
- }//class
Advertisement
Add Comment
Please, Sign In to add comment