为什么这要求用户输入两次?

时间:2019-11-07 08:21:06

标签: while-loop user-input

public static void main(String[] args) {

    Scanner userInput = new Scanner(System.in);
    String choice = "";
    while(true) {
        System.out.println("What would you like to do? Enter help for commands.");
        choice = userInput.nextLine();
        switch(choice.toLowerCase()) {
            case "turn right":
                System.out.println("right");
            case "turn left":
                System.out.println("left");
            case "move":
                System.out.println("move");
        }
    }

在循环的每次迭代中,它都会两次请求用户输入。例如,在到达switch语句之前,它需要两行。为什么会这样?

1 个答案:

答案 0 :(得分:1)

Eclipse出现故障,我的代码运行不正确。通过关闭并重新打开Eclipse进行修复。