带有字符串比较的循环不起作用?

-1 java string compare

我刚开始学习 Java... 对不起,如果这只是一个太愚蠢的问题。

我试图比较用户输入。如果输入不是“是”或“否”,则强制用户输入其中之一......但我的代码不起作用......

编译没有问题,但即使输入是“是”或“否”,while 循环也会继续循环。

尝试在循环中打印出“userInput”的值,但在输入时正确显示“是”或“否”,但循环仍在继续。

protected static boolean askUser() {
        String userInput = "x";
        boolean userChoice;
        System.out.println("Do you have a question you want to know the answer too? (Yes/No): ");
        userInput = input.nextLine();

        while (!userInput.equalsIgnoreCase("Yes") || !userInput.equalsIgnoreCase("No")) {
            System.out.println("Please input only \"Yes\" or \"No\": ");
            userInput = input.nextLine();
        }

        if (userInput.equalsIgnoreCase("Yes")) {
            userChoice = true;
        } else {
            userChoice = false;
        }

        return userChoice;
    }
Run Code Online (Sandbox Code Playgroud)

关于如何修复此代码的任何想法?

Mik*_*CAT 5

!userInput.equalsIgnoreCase("Yes") || !userInput.equalsIgnoreCase("No")永远是正确的,因为YesNoNo是不是Yes

您将希望在输入不是Yes 不是时循环No,因此条件应该是!userInput.equalsIgnoreCase("Yes") && !userInput.equalsIgnoreCase("No")