Advertisement
brilliant_moves

EvenEntryLoop.java

Feb 18th, 2015
1,904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.49 KB | None | 0 0
  1. /*
  2. asked on YA:
  3.  
  4. Can anyone code this Java program for me?
  5. Write an application that asks a user to type an even number to continue or to type 999 to stop.
  6.  When the user types an even number, display the message “Good job!” and then ask for another input.
  7.  When the user types an odd number, display an error message and then ask for another input.
  8.  When the user types 999, end the program. Use JOptionPane for the user input Save the file as EvenEntryLoop.java.
  9.  
  10. */
  11.  
  12. import javax.swing.JOptionPane;
  13.  
  14. public class EvenEntryLoop {
  15.  
  16.     /**
  17.     *   Program:    EvenEntryLoop.java
  18.     *   Purpose:    Yahoo! Answers
  19.     *   Creator:    Chris Clarke, author "50 Java Program Source Codes"
  20.     *            & "50 More Java Source Codes" available in paperback
  21.     *            & e-book at ridiculously low prices from amazon.co.uk
  22.     *   Created:    18.02.2015
  23.     */
  24.  
  25.     public static void main(String[] args) {
  26.  
  27.         final int SENTINEL = 999;
  28.         JOptionPane pane = new JOptionPane();
  29.         int number = 0;
  30.  
  31.         do {
  32.             try {
  33.                 number = Integer.parseInt( pane.showInputDialog(
  34.                  "Please type an even number, or type 999 to stop."));
  35.             } catch (NumberFormatException e) {
  36.                 pane.showMessageDialog( null, "Error! That isn't a whole number!");
  37.                 continue;
  38.             } // try
  39.  
  40.             if (number==SENTINEL) System.exit(0);
  41.  
  42.             if (number%2==0) {
  43.                 pane.showMessageDialog( null, "Good job!");
  44.             } else {
  45.                 pane.showMessageDialog( null, "Error! That isn't an even number.");
  46.             } // if
  47.  
  48.         } while (true);
  49.  
  50.     } // main()
  51.  
  52. } // class EvenEntryLoop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement