Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- asked on YA:
- Can anyone code this Java program for me?
- Write an application that asks a user to type an even number to continue or to type 999 to stop.
- When the user types an even number, display the message “Good job!” and then ask for another input.
- When the user types an odd number, display an error message and then ask for another input.
- When the user types 999, end the program. Use JOptionPane for the user input Save the file as EvenEntryLoop.java.
- */
- import javax.swing.JOptionPane;
- public class EvenEntryLoop {
- /**
- * Program: EvenEntryLoop.java
- * Purpose: Yahoo! Answers
- * Creator: Chris Clarke, author "50 Java Program Source Codes"
- * & "50 More Java Source Codes" available in paperback
- * & e-book at ridiculously low prices from amazon.co.uk
- * Created: 18.02.2015
- */
- public static void main(String[] args) {
- final int SENTINEL = 999;
- JOptionPane pane = new JOptionPane();
- int number = 0;
- do {
- try {
- number = Integer.parseInt( pane.showInputDialog(
- "Please type an even number, or type 999 to stop."));
- } catch (NumberFormatException e) {
- pane.showMessageDialog( null, "Error! That isn't a whole number!");
- continue;
- } // try
- if (number==SENTINEL) System.exit(0);
- if (number%2==0) {
- pane.showMessageDialog( null, "Good job!");
- } else {
- pane.showMessageDialog( null, "Error! That isn't an even number.");
- } // if
- } while (true);
- } // main()
- } // class EvenEntryLoop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement