Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainMenu {
- public static void main(String[] args)
- {
- System.out.println("My First Java program can do many things!");
- System.out.println("1.Estimate populationn2.Generate random integern3. Print ASCII tablen4. Approximate pi by iteration");
- System.out.println("What would you like to do? (1-4)");
- System.out.print("Your choice: ");
- int input = 0;
- Scanner keyboard = new Scanner(System.in);
- switch (keyboard.nextInt())
- {
- case 1:
- System.out.println("You chose to estimate population.");
- break;
- case 2:
- System.out.println("You chose to generate random integer.");
- break;
- case 3:
- System.out.println("You chose to print ASCII table.");
- break;
- case 4:
- System.out.println("You chose to approximate pi by iteration.");
- break;
- }
- if(input>4 || input<1)
- {
- System.out.println("Sorry, I don't know what to do. Please try again.");
- keyboard.next();
- if(!keyboard.hasNextInt())
- {
- System.out.println("Sorry, only integers allowed for this menu. Good-bye!");
- System.exit(0);
- }
- keyboard.next();
- input = keyboard.nextInt();
- keyboard.nextLine();
- }
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class Test {
- public static void main(String[] args) {
- System.out.println("My First Java program can do many things!");
- System.out.println("1.Estimate populationn2.Generate random integern3. Print ASCII tablen4. Approximate pi by iteration");
- System.out.println("What would you like to do? (1-4)");
- System.out.print("Your choice: ");
- Scanner keyboard = new Scanner(System.in);
- while (true) {
- int input = 0;
- try {
- input = keyboard.nextInt();
- } catch (InputMismatchException e) {
- System.out
- .println("Sorry, only integers allowed for this menu. Good-bye!");
- System.exit(0);
- }
- switch (input) {
- case 1:
- System.out.println("You chose to estimate population.");
- break;
- case 2:
- System.out.println("You chose to generate random integer.");
- break;
- case 3:
- System.out.println("You chose to print ASCII table.");
- break;
- case 4:
- System.out.println("You chose to approximate pi by iteration.");
- break;
- }
- if (input > 4 || input < 1) {
- System.out
- .println("Sorry, I don't know what to do. Please try again.");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment