java - 循环验证

标签 java loops

我正在制作一个基于文本的冒险游戏,我需要进行一些用户输入验证。
我已经为此特定实例设置了循环,但在输入正确的输入后似乎无法中断循环。

代码:

    final String KNOCK;
    final String SLAM;
    System.out.println("Do you politely knock on the doors or do you slam them over?");
    System.out.println("Knock or Slam?");
    String decisionOne = userInput.nextLine().toUpperCase();
                
    if (decisionOne.equals("KNOCK"))
        System.out.println("You politely knock on the door. After a minute you decide to push on the handle.  The oak doors swing open, the old rusted iron hinges screaming into the dimly lit hallway in front of you. The Hall is roughly fifty feet long. Three doors on the East wall, and two doors on the west wall. The smell of mold, moisture, and iron hang thick in the air. What used to be a lush red carpet lines the entire length of the hallway up to a narrow set of stairs that reach up to another floor.");
        else if (decisionOne.equals("SLAM"))
            System.out.println("You slam into the heavy oak doors, the heavy iron hinges giving way with a pop. Dust settles around your head as you peer into the first room of The Keep. The Hall is roughly fifty feet long. Three doors on the western wall, and two doors on the eastern wall. The smell of mold, moisture, and iron hang thick in the air. What used to be a lush red carpet lines the entire length of the hallway up to a narrow set of stairs that reach up to another floor.");
        else {
            KNOCK = "KNOCK";
            SLAM = "SLAM";
            do {
                System.out.println("Your options are Knock or Slam. Don't be a rude.");
                decisionOne = userInput.nextLine().toUpperCase();
                if (decisionOne.equals("KNOCK"))
                    System.out.println("You politely knock on the door. After a minute you decide to push on the handle.  The oak doors swing open, the old rusted iron hinges screaming into the dimly lit hallway in front of you. The Hall is roughly fifty feet long. Three doors on the East wall, and two doors on the west wall. The smell of mold, moisture, and iron hang thick in the air. What used to be a lush red carpet lines the entire length of the hallway up to a narrow set of stairs that reach up to another floor.");
                else if (decisionOne.equals("SLAM"))
                    System.out.println("You slam into the heavy oak doors, the heavy iron hinges giving way with a pop. Dust settles around your head as you peer into the first room of The Keep. The Hall is roughly fifty feet long. Three doors on the western wall, and two doors on the eastern wall. The smell of mold, moisture, and iron hang thick in the air. What used to be a lush red carpet lines the entire length of the hallway up to a narrow set of stairs that reach up to another floor.");
            } while ((!decisionOne.equals("KNOCK")) || (!decisionOne.equals("SLAM")));
        }

这就是我的输出:

Your options are Knock or Slam. Don't be a rude.

slam

You slam into the heavy oak doors, the heavy iron hinges giving way with a pop. Dust settles around your head as you peer into the first room of The Keep. The Hall is roughly fifty feet long. Three doors on the western wall, and two doors on the eastern wall. The smell of mold, moisture, and iron hang thick in the air. What used to be a lush red carpet lines the entire length of the hallway up to a narrow set of stairs that reach up to another floor. Your options are Knock or Slam. Don't be a rude.

问题是,如果您第一次输入正确的值,那么代码将正常进行。如果您先输入不正确的值,然后尝试输入正确的值,则循环不会中断并继续要求用户输入,而不会继续执行代码的下一步。

非常感谢任何帮助!!

谢谢

最佳答案

只要此条件为 true,您的 do 循环就会运行:

while ((!decisionOne.equals("KNOCK")) || (!decisionOne.equals("SLAM")))

用英语读给自己听......“虽然 DecisionOne 不等于 KNOCK 或 DecisionOne 不等于 SLAM”。

假设decisionOne是KNOCK。条件保持为 true,因为 decisionOne 不等于 SLAM,并且循环将永远继续下去。

您需要一个and条件:

while ((!decisionOne.equals("KNOCK")) && (!decisionOne.equals("SLAM")))

关于java - 循环验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59056325/

相关文章:

java - 如何获取像 "method1.method2"ClassLoader这样的方法?

css - Wordpress post 循环排在第一位

python - 迭代列表元组

c - 如何删除 C 中的以下 lint 警告?

java字符串连接

java - 公共(public)静态方法 - 一个坏兆头?

css - 循环多个CSS动画

r - 如果for循环中发生错误,如何更改值?

java - 单击 List<WebElement> 中找到的所有元素

java - 为什么我会收到 Stackoverflow 错误?