Question #2949

How do I write a program on java that allows a user to choose from a menu of options if the user does not select one of the choices on the menu have the program reprint the menu and ask the user to enter their selection again the program should continue to repeat the menu until the user has selected a valid option once a valid option has been selected the program should print the users selection

Expert's answer

import java.util.*;

public class Menu {
& public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("1) action 1");
System.out.println("2) action 2");
System.out.println("3) action 3");
System.out.println("4) action 4");
System.out.println("5) exit");
switch (scan.nextInt()) {
case 1: {
& System.out.println("action 1 selected");
& break;
}
case 2: {
& System.out.println("action 2 selected");
& break;
}
case 3: {
& System.out.println("action 3 selected");
& break;
}
case 4: {
& System.out.println("action 4 selected");
& break;
}
case 5: {
& return;
}
default: {
& System.out.println("bad selection. try again");
& break;

}

}

}
& }
}
LATEST TUTORIALS
APPROVED BY CLIENTS