【问题标题】:While loop does not wait for if statement's completion [duplicate]While循环不等待if语句的完成[重复]
【发布时间】:2014-03-06 17:03:03
【问题描述】:

我正在为 Intro to CS 做一个学校项目,但我无法发布我的代码,因为我们的老师让我们无法从 StackOverflow 中检查代码。我的问题是我的代码格式为:

while (condition){
Do something;

if (new condition){
Do something else;
Wait for input;
Depending on input make while's condition false;
}

当 if 语句被评估并执行其他操作时,此代码应等待输入。但是,我的代码不会等待“等待输入”步骤,而是直接进入“做某事”步骤。这是一些代码。感谢您的帮助。

while (inum != 102) {
System.out.println("Enter a number between 1 and 100: ");
inum = input.nextInt();
else if (inum == 101) {
                System.out.println("Are you sure you want to quit?");
                confirm = input.nextLine();
                if (confirm == "yes") {
                    inum = 102;
                }
}

当我输入 101 时,这里的代码给了我这个: 你确定你要退出吗? 输入一个介于 1 到 100 之间的数字:

*代码不等待

confirm = input.nextLine();
                if (confirm == "yes") {
                    inum = 102;
                } 

步骤。

【问题讨论】:

  • @SotiriosDelimanolis 和 else-if 没有任何以前的 if 显示在代码中
  • SO 上最常被问到的问题,看来... 将if (confirm == "yes") 替换为if ("yes".equals(confirm))
  • 更改为 ("yes".equals(confirm)) 后仍然得到相同的响应
  • 您的牙套和 else if 的位置有些有趣。你能检查你的示例编译和必要的更新吗?

标签: java loops if-statement while-loop


【解决方案1】:

解决问题的最简单方法是调用

input.nextLine();

就在下面

inum = input.nextInt();

原因是:当你在控制台输入“101”时,你真的输入了 101 和 NEWLINE。 nextInt() 从控制台缓冲区获取 101,但 NEWLINE 字符仍然存在。因此,您的代码中的第二个 nextLine() 被跳过(代码假设您输入了一个新的空行)

【讨论】:

    【解决方案2】:
    1. 您应该使用confirm.equals("yes")。
    2. input 已经用于输入数字并声明另一个扫描器对象来输入确认字符串。

    【讨论】:

      【解决方案3】:

      定义另一个输入扫描器,不要对 String 和 int 使用相同的扫描器 试试这段代码(我把 System.exist(0) 这样当你输入yes时程序退出)

      import java.util.Scanner;
      
      public class Test {
      
      /**
       * @param args
       */
      public static void main(String[] args) {
          int inum = 0;
          Scanner input = new Scanner(System.in);
          Scanner input1 = new Scanner(System.in);
          while (inum != 102) {
              System.out.println("Enter a number between 1 and 100: ");
              inum =  input.nextInt();
              if (inum == 101) {
                  System.out.println("Are you sure you want to quit?");
                  String confirm = input1.nextLine();
                  if (confirm.equalsIgnoreCase("yes")) {
                      inum = 102;
                      System.exit(0);
                  }
              }
      
          }
      }
      

      【讨论】:

      • confirm == "yes" 不起作用。
      • 为什么-1我只是忘了把confirm.equalsIgnoreCase("yes")
      • 提醒您您的回答没有帮助。好像成功了。
      • 您需要 System.exit(0) 吗?代码现在不会退出while循环吗?如果你不关心特殊的 inum ==102 情况,你可以简单地做while (true)
      • 它可以工作,但是当我像我说的那样输入代码时,我忘了用 equals 更改 ==
      【解决方案4】:
      if( StringUtils.equals(confirm, "yes") ) { ...
      

      【讨论】:

        猜你喜欢
        • 2015-05-19
        • 1970-01-01
        • 2012-12-09
        • 2018-03-28
        • 1970-01-01
        • 2013-06-09
        • 2014-01-21
        • 2015-11-08
        • 2016-01-12
        相关资源
        最近更新 更多