循环未结束

时间:2018-10-12 02:24:50

标签: java arrays if-statement while-loop

我正在尝试循环此if语句,直到用户输入正确的答案为止。即使我输入正确的输入,运行时也不会结束。另外,我如何在while循环外使用playerPick变量而不弄乱代码?

System.out.println("Please choose either Applaro, Svartra, Tunholmen, or Godafton.");
boolean temp = true;
    while (temp) {
        String playerPick = console.next();
        if (!playerPick.equals("Applaro") && !playerPick.equals("Svartra") && !playerPick.equals("Tunholmen")
                && !playerPick.equals("Godafton")) {
            System.out.println("Please enter a valid input.");
        } else if (playerPick.equals("Applaro") || playerPick.equals("Svartra") || playerPick.equals("Tunholmen")
                || playerPick.equals("Godafton")) {
            System.out.println("You have picked " + playerPick);
        }
    }

3 个答案:

答案 0 :(得分:3)

只需设置temp = false,如下例所示:

System.out.println("Please choose either Applaro, Svartra, Tunholmen, or Godafton.");
    boolean temp = true;
        while (temp) {
            String playerPick = console.next();
            if (!playerPick.equals("Applaro") && !playerPick.equals("Svartra") && !playerPick.equals("Tunholmen")
                    && !playerPick.equals("Godafton")) {
                System.out.println("Please enter a valid input.");
            } else if (playerPick.equals("Applaro") || playerPick.equals("Svartra") || playerPick.equals("Tunholmen")
                    || playerPick.equals("Godafton")) {
                System.out.println("You have picked " + playerPick);
                //here
                temp = false;
            }
        }

答案 1 :(得分:1)

只要 temp 为true,while循环就会继续。在嵌套的if语句中,我们将 temp 设置为false,从而退出循环。

对于 playerPick :该变量在循环外部声明,因此应该可以在声明下方的函数内的任何位置访问(代码从上向下读取)。如果您尝试通过其他方法访问 playerPick ,则需要在超类中声明变量。

注意:我还清理了您的if语句,使其更具可读性。

希望这会有所帮助!

System.out.println("Please choose either Applaro, Svartra, Tunholmen, or Godafton.");
boolean temp = true;
String playerPick = "";
    while (temp) {
        playerPick = console.next();
        if (playerPick.equals("Applaro") || playerPick.equals("Svartra") || playerPick.equals("Tunholmen") || playerPick.equals("Godafton")) {
            System.out.println("You have picked " + playerPick);
            temp = false;
        }
        else
            System.out.println("Please enter a valid input.");
    }

答案 2 :(得分:-1)

这可以删除可读性较低的变量“ temp”。
执行while循环会更好地提示用户输入提示。
我认为在while循环中删除if-else子句更具可读性。

public static void main(String[] args) {
    String prompt = "Please choose either Applaro, Svartra, Tunholmen, or Godafton.";
    String playerPick = null;
    try (Scanner console = new Scanner(System.in)) {
        do {
            System.out.println(prompt);
            playerPick = console.next();                   
            prompt = "Please enter a valid input.";
        } while (!playerPick.equals("Applaro") && !playerPick.equals("Svartra") && !playerPick.equals("Tunholmen") && !playerPick.equals("Godafton"));
        System.out.println("You have picked " + playerPick);

    } catch(Exception ex) {
        System.out.println("Please enter a valid input.");
    }        
}