如何继续执行子菜单';sJava终端菜单
这是目前的菜单。我想创建要发货和订购的子菜单。 我真的很想知道怎么做。当我选择船舶备选方案时,我希望另一个菜单显示不同的船舶,具体为10艘 最好的方法是什么 提前谢谢如何继续执行子菜单';sJava终端菜单,java,oop,menu,java.util.scanner,Java,Oop,Menu,Java.util.scanner,这是目前的菜单。我想创建要发货和订购的子菜单。 我真的很想知道怎么做。当我选择船舶备选方案时,我希望另一个菜单显示不同的船舶,具体为10艘 最好的方法是什么 提前谢谢 import java.util.Scanner; public class Main { boolean exit; public static void main(String[] args) { Main main = new Main(); main.runMenu(); } public voi
import java.util.Scanner;
public class Main {
boolean exit;
public static void main(String[] args) {
Main main = new Main();
main.runMenu();
}
public void runMenu() {
printHeader();
while(!exit) {
printMenu();
int choice = getInput();
performAction(choice);
}
}
private void printHeader() {
System.out.println("+-----------------------------------+");
System.out.println("| Welcome operator! |");
System.out.println("+-----------------------------------+");
}
private void printMenu() {
System.out.println("\nMake your selection");
System.out.println("1) Ship");
System.out.println("2) Order");
System.out.println("3) Map");
System.out.println("4) Status");
System.out.println("0) Exit");
}
private int getInput() {
Scanner kb = new Scanner(System.in);
int choice = -1;
while(choice < 0 || choice > 4) {
try {
System.out.print("\nEnter your choice");
choice = Integer.parseInt(kb.nextLine());
}
catch (NumberFormatException e) {
System.out.println("Invalid selection. Please try again");
}
}
return choice;
}
如果这不是你想要的,告诉我 用户做出选择后,可以使用清除控制台
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
然后打印出下一个子菜单
例如(此方法可能非常冗长,但适用于简单的文本菜单):
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
public static void main(String[] args) {
while (true) {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
System.out.println("1. Food");
System.out.println("2. Drink");
System.out.println("3. Quit");
//get user input
if (userInput == 1) { //food submenu (submenus can be easily put in functions)
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
System.out.println("1. Apple");
System.out.println("2. Banana");
System.out.println("3. Back");
//get and process input
} else if (userInput == 2) { //drink submenu (submenus can be easily put in functions)
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
System.out.println("1. Water");
System.out.println("2. Juice");
System.out.println("3. Back");
//get and process input
} else break;
}
}