首页 > 解决方案 > 在菜单中创建子菜单是否有更有效的方法

问题描述

我现在正在学习java,我想知道是否有一种更简单的方法可以在菜单中创建子菜单,而无需将菜单硬编码到这个项目中。

System.out.println("Enter one of the following commands:");
System.out.println("1 - List todos");
System.out.println("2 - Add todo");
Scanner scanchoice = new Scanner(System.in);
System.out.println();
System.out.println("Enter \"1\", \"2\" ,\"3\", \"4\" or \"5\"");
int choiceentry = scanchoice.nextInt();

while (choiceentry != 5) {
    if (choiceentry < 1 || choiceentry > 5) {
        System.out.println("Enter \"1\", \"2\", \"3\", \"4\"or \"5\"");
        choiceentry = scanchoice.nextInt();
    } else if (choiceentry == 2) {
        Scanner EnterCategory = new Scanner(System.in);
        System.out.println("Select an Item to between 1 and 6 ");
        System.out.println("Enter one of the following commands:");
        System.out.println("1 - Red");
        System.out.println("2 - White");
        System.out.println("3 - Blue");
        Scanner Colorchoice = new Scanner(System.in);
        System.out.println();
        System.out.println("Enter \"1\", \"2\" or \"3\"");
        String ColorChoiceEntry = Colorchoice.next();
        while (ColorChoiceEntry != 3) {
            if (ColorChoiceEntry < 1 || ColorChoiceEntry > 6) {
                System.out.println("Enter \"1\", \"2\", \"3\",\"4\" ,\"5\"or \"6\"");
                choiceentry = scanchoice.nextInt();
            } else if (choiceentry == 1) {
                SystemColor.getColor(ColorChoiceEntry).getRed();
            } else if (choiceentry == 2) {
                //..something else
                SystemColor.getColor(ColorChoiceEntry).getWhite();
            }
        }
    }
}

这是我到目前为止所做的(但有更多选择

标签: javamenu

解决方案


推荐阅读