If the user types an integer which is not between 1 and 6, then your program must print an error message "Unknown action!" to the user (hint: when testing the integer for the action, use the default case of a switch statement) and then print the menu again (by just going back to the beginning of the while loop).Java代码
时间: 2023-06-24 19:05:40 浏览: 54
下面是一个示例Java代码实现:
```java
import java.util.Scanner;
public class Menu {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int action = 0;
while (true) {
System.out.println("Please select an action:");
System.out.println("1. Action A");
System.out.println("2. Action B");
System.out.println("3. Action C");
System.out.println("4. Action D");
System.out.println("5. Action E");
System.out.println("6. Action F");
System.out.print("Enter the number of the action you want to perform: ");
action = scanner.nextInt();
switch (action) {
case 1:
System.out.println("Performing Action A");
break;
case 2:
System.out.println("Performing Action B");
break;
case 3:
System.out.println("Performing Action C");
break;
case 4:
System.out.println("Performing Action D");
break;
case 5:
System.out.println("Performing Action E");
break;
case 6:
System.out.println("Performing Action F");
break;
default:
System.out.println("Unknown action!");
continue;
}
System.out.println("Action completed.\n");
}
}
}
```
这个程序将不断循环,直到用户输入一个无效的操作。如果用户输入1到6之间的整数,则将执行相应的操作并打印一条消息,然后重新显示菜单。否则,它将打印"Unknown action!"错误消息,然后继续循环。