如果编写了某个命令,如何跳过 if 语句

How to skip an if statement if a certain command is written

我正在制作一个基于文本的冒险游戏 "game" 作为练习,(我意识到这可能是正确的做法)并且我希望用户能够输入 'Command'查看他当前可以使用的命令 - 如果他们已经知道这些命令,我​​希望他们能够直接输入命令本身(在本例中为 1、2 或 3)。但是,我遇到的问题是,如果用户输入 'Command',他们之后将无法使用它们(1,2 或 3)。我知道我可以使用其他扫描仪,但我在这里尽量避免使用它。

    out.print("\nWhat do you want to do? *Type 'Commands' to look through your options.*\n");

    String playerInput = userInput.nextLine();
    if (playerInput.equals("Commands")){
        out.println("\nCommands\n"
                + "(1) - Inspect\n"
                + "(2) - Explore\n"
                + "(3) - Inventory\n");
    }

    if (playerInput.equals("1")) {
        out.print("You find a box under your bed. \nDo you want to open it?  (Y/N)\n");

        String playerAnswer = userAnswer.nextLine();
        if (playerAnswer.equals("Y")) {
            out.println("Inside the box you find a photograph");
    }
        // Another if statement with option 2 here
}

循环直到满足某些退出条件。请注意,每次调用 userInput.nextLine() 时,它都会等待用户输入新的文本行并将其分配给 playerInput,然后再继续。

String playerInput = "";
out.print("\nWhat do you want to do? *Type 'Commands' to look through your options.*\n");
while(! playerInput.equals("Exit")){
    playerInput = userInput.nextLine();
    if (playerInput.equals("Commands")){
        out.println("\nCommands\n"
                + "(1) - Inspect\n"
                + "(2) - Explore\n"
                + "(3) - Inventory\n"
                + "Exit - Quits the game\n");
    }

    if (playerInput.equals("1")) {
        out.print("You find a box under your bed. \nDo you want to open it?  (Y/N)\n");

        String playerAnswer = userAnswer.nextLine();
        if (playerAnswer.equals("Y")) {
            out.println("Inside the box you find a photograph");
    }

    //More if statements...
}