当前位置: 首页 > 知识库问答 >
问题:

循环验证[重复]

宋安晏
2023-03-14

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

代码:

    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")));
        }

我的输出是这样的:

你的选择是敲打还是猛击。别那么粗鲁。

大满贯

你猛地撞上沉重的橡木门,沉重的铁铰链砰的一声让位了。当你凝视城堡的第一个房间时,灰尘落在你的头上。大厅大约有五十英尺长。西墙有三扇门,东墙有两扇门。空气中弥漫着霉菌、湿气和铁的味道。曾经郁郁葱葱的红地毯沿着走廊一直延伸到一组狭窄的楼梯,一直延伸到另一层。你的选择是敲门或猛击。不要粗鲁。

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

任何帮助都非常感谢!

谢谢你

共有3个答案

湛博易
2023-03-14

如果您先输入不正确的值,然后尝试输入正确的值,循环不会中断,并继续要求用户输入,而不会进入代码的下一步。

更改:

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

到:

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

您只希望它在输入不等于“敲打”和“砰击”时继续循环。有了|(或)在其中,即使用户输入其中一个值,它也会循环运行。。。

魏风华
2023-03-14

循环的部分将始终求值为true

尝试:

while(!decisionOne.Equals("KNOCK") && !decisionOne.Equals("SLAM"));
边明煦
2023-03-14

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

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

用英语读给自己听...“而决定一不等于敲门或决定一不等于SLAM”。

假设decisionOne是敲门。该条件保持为true,因为decisionOne不等于SLAM,循环将永远持续。

您需要一个条件:

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

 类似资料:
  • 我正在创建一个简单的21点java程序,我被while循环输入验证难住了。当要求用户抽牌时,他/她可以选择:(是/否)以及他们是否想再次玩。我的问题是,当我被提示抽牌时,我选择y,该字符只应提示抽牌。。。但似乎任何角色都会这样做。如果我想再次比赛,同样的情况也会发生。此外,当提示我再次播放时,我想返回while循环的开始,但我似乎无法调用该函数。我需要帮助!这是我的代码:

  • 我有此代码来验证用户输入。条件:输入的值只能是零或正数。不接受负值和字母字符。 这是我的代码,它一直在循环:

  • 问题内容: 我正在使用Java Swing编写游戏。我想在每次循环执行时绘制一下,并在之间稍加延迟以在屏幕上创建级联效果。我相信系统中的效率例程会将调用折叠为一个调用。无论如何,所有更改都在总延迟后立即发生。是否有某种方法可以强制系统立即重新绘制,然后在循环的每次迭代中延迟? 我的代码: 问题答案: 您可以用来强制立即重绘。 编辑:再次阅读您的问题后,对我来说,您可能正在事件分发线程上执行逻辑。这

  • 我已经使用extend from创建了自定义规则。所以我有这个 当为false且为true时,从上述代码中,它会被上一条规则覆盖,因此结果始终被视为true而不是false 那么我如何才能做到这一点呢? 规则:

  • 我有一个类,其中我们将“两个时态对象之间的时间量”存储到一个长变量monthBt中,并将其用作“for循环”中的测试表达式。然而,我在azure sechub中收到中级安全警告“未检查循环条件的输入”。 验证和删除警告的最佳方法是什么?谢谢

  • 以上将给出以下随机输出: 任务很简单:确保每个promise只在另一个promise之后运行()。 由于某种原因,我找不到一个方法来做这件事。 我尝试了生成器函数(),尝试了返回promise的简单函数,但最终都归结为同一个问题:循环是同步的。 对于async,我只需使用。 你怎么解决?