1

我尝试制作一个菜单系统,让用户在四个选项之间进行选择。为了区分选择,我检查了输入的 int。它有效,但不知何故我觉得它不是很优雅。特别是当我将 selectedMenu 的初始值设置为 1902475424 以检查用户何时输入了不匹配的值时。我假设用户不会不小心输入 1902475424。

有没有一种更简单的方法来制作菜单系统,或者会这样做吗?这个主要有缺陷吗?

是的,我是 Java 的初学者 :-)

import java.util.Scanner;
import java.util.InputMismatchException;

public class Menu {

    public void printMenu() {
        System.out.println(
                "1. Start new game\n" + 
                "2. Load game\n" + 
                "3. Settings\n" + 
                "4. Exit\n"
        );
    }

    public void selectMenu() throws InputMismatchException {
        int selectedMenu = 1902475424;
        Scanner aScanner = new Scanner(System.in);
        do {
            selectedMenu = 1902475424;
            try {
                System.out.println("Try block begin.");
                selectedMenu = aScanner.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("Catch blok begin.");
                System.out
                        .println("Invalid input, please input a number between 1-4.");
                aScanner.nextLine();
            }
            if ((selectedMenu < 1 || selectedMenu > 4)
                    && (selectedMenu != 1902475424)) {
                System.out.println("Input out of range \"" + selectedMenu
                        + "\". Input a number between 1-4.");
            }
        } while (selectedMenu == 1902475424
                || (selectedMenu < 1 || selectedMenu > 4));

        if (selectedMenu >= 1 && selectedMenu <= 4) {
            System.out.println("A new game will now start.");
        }
    }
}
4

4 回答 4

1

您的方法倾向于过度杀伤类别:]
您可以取消您的随机值,1902475424如下所示:

public void selectMenu() throws InputMismatchException {
    int selectedMenu;
    Scanner aScanner = new Scanner(System.in);
    do {
        try {
            System.out.println("Try block begin.");
            selectedMenu = aScanner.nextInt();
            if(selectedMenu < 1 || selectedMenu > 4) {
                System.out.println("Input out of range \"" + selectedMenu + "\". Input..");
            }
        } catch(InputMismatchException e) {
            System.out.println("Catch blok begin.");
            System.out.println("Invalid input, please input a number between 1-4.");
            aScanner.nextLine();
            selectedMenu = 0;
        } 

    } while(selectedMenu < 1 || selectedMenu > 4);

    System.out.println("A new game will now start.");
}
于 2013-01-04T22:25:41.123 回答
1

考虑以下替代方案(伪代码):

int getMenuOption() {
    print(message)
    read(input)

    if input is valid then return input
    else then return getMenuOption()
}

这是递归的,所以如果用户坐在那里并输入错误的数字足够长,您可能会溢出堆栈。您可以轻松地对其进行扩充,为用户提供固定次数的尝试:

int getMenuOption(int triesRemaining) {
    if (triesRemaining == 0) throw new RetriesExceededException();

    print(message)
    read(input)

    if input is valid then return input
    else then return getMenuOption(triesRemaining - 1)
}
于 2013-01-04T22:27:56.947 回答
0

这是对您提供的方法的修订selectMenu(),应该可以完成工作!我对其进行了测试,它似乎按预期工作。:)

public void selectMenu() {

    int selectedMenuItem = 0;
    Scanner aScanner = new Scanner(System.in);

    while(selectedMenuItem == 0){

        String userInputMenuItemString = aScanner.nextLine();

        try {
            int userInputMenuItem = Integer.parseInt(userInputMenuItemString);
            if(userInputMenuItem > 0 && userInputMenuItem <= 4){

                selectedMenuItem = userInputMenuItem;

            }else{

                System.out.println("No option #" + Integer.toString(userInputMenuItem) + " exists!\nTry again:");

            }
        } catch(NumberFormatException ex) {

            System.out.println("Please input a number!");

        }

    }

    switch(selectedMenuItem){
        case 1:
            System.out.println("You chose to start a new game!");
            break;
        case 2:
            System.out.println("You chose to load a game!");
            break;
        case 3:
            System.out.println("You chose to access settings!");
            break;
        case 4:
            System.out.println("You chose to exit. Bye!");
            System.exit(0);
            break;
    }
}
于 2013-01-04T22:28:24.587 回答
0

尝试类似的东西(我还没有测试过)

    import java.util.Scanner;
    import java.util.InputMismatchException;

    public class Menu {

            public void printMenu() {
                    System.out.println("1. Start new game\n" + "2. Load game\n"
                                    + "3. Settings\n" + "4. Exit\n");
            }

            public void selectMenu() throws InputMismatchException {
                    int selectedMenu;
                    boolean validSelection = false;
                    Scanner aScanner = new Scanner(System.in);
                    while(!validSelection) {
                            selectedMenu = aScanner.nextInt();
                            validSelection = true;
                            switch (selectedMenu) {
                                    case 1:
                                            // doWhen1();
                                            break;
                                    case 2:
                                            // doWhen2();
                                            break;
                                    case 3:
                                            // doWhen3();
                                            break;
                                    case 4:
                                            // doWhen4();
                                            break;
                                    default:
                                            System.out.println("Input out of range \"" + selectedMenu
                                                    + "\". Input a number between 1-4.");
                                            validSelection = false;
                            }
                    }
            }
    }
于 2013-01-04T22:30:44.083 回答