【问题标题】:Why does this while loop execute infinite times?为什么这个while循环会无限执行?
【发布时间】:2014-08-31 07:50:50
【问题描述】:

我有一个 while 循环,它应该读取数组的元素以尝试找到给定 variable 的值,但是,循环无限次执行自己,我不明白为什么。一旦找到它正在寻找的值,它就应该退出;我知道它确实找到了它正在寻找的东西,因为它会无限次打印出I've found it!。到目前为止,该方法的代码是:

try{
    System.out.println("Enter your card number to access your account:");
    int CardNumber = sc.nextInt();
    String CardNumberStr = Integer.toString(CardNumber);
    boolean Exist = false;
    String LineNo;
    String [] CardNum = {};
    int Counter;
    FileReader fileReader = new FileReader("VirtualATM.txt");
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    line = bufferedReader.readLine();
    CardNum = line.split("\\s+");
    do{
        for(Counter = 0; Counter < CardNum.length; Counter++){
            LineNo = CardNum[Counter];
            if(LineNo.contains(CardNumberStr)){
                Exist = true;
                System.out.println("I've found it!");
            }
            else if(Counter == CardNum.length){
                Exist=false;
            }
        }
    }while(Exist = false || line != null);
    bufferedReader.close();
}catch(FileNotFoundException e){
    e.printStackTrace();
    System.out.println(e.getMessage());
}catch(IOException e){
    e.printStackTrace();
    System.out.println(e.getMessage());
}

谁能帮我弄清楚为什么会这样?

【问题讨论】:

  • 我建议您设置 Eclipse(或您使用的任何 IDE)在 if/while 块中有布尔赋值时发出警告。
  • 可能是 while (Exist == false... 而不是 Exist=false ????

标签: java do-while


【解决方案1】:

因为您在 do-while 循环中分配了 Exist = false。应该是Exists == false 或者更好:!Exist

} while(!Exist || line != null);

除此之外,请遵循Java Code Conventions(旧但仍在使用),其中变量应使用驼峰式大小写但以小写字母开头。


查看更多代码,您永远不会阅读文件的另一行,do-while 的逻辑应该使用 AND (&amp;&amp;),而不是 OR (||)。只需将其添加到您的代码中:

line = bufferedReader.readLine();
CardNum = line.split("\\s+");
do{
    for(Counter = 0; Counter < CardNum.length; Counter++){
        LineNo = CardNum[Counter];
        if(LineNo.contains(CardNumberStr)){
            Exist = true;
            System.out.println("I've found it!");
        }
        else if(Counter == CardNum.length){
            Exist=false;
        }
    }
    //add this line to read another line of the file
    //and check if it exists
    line = bufferedReader.readLine();
} while(!Exist && line != null);

【讨论】:

  • 我已经改变了它仍然会无限次循环??
  • 太好了!谢谢。
【解决方案2】:

您不会在循环中重新读取您的 line 变量,因此 line != null 始终为真。

【讨论】:

  • +1,很好的捕捉,甚至滑过我的眼睛。浏览粘贴的代码后看不到这一点:-)
【解决方案3】:

Exist = false 是这里万恶之源。 = 是赋值运算符,== 是相等比较运算符。

【讨论】:

    【解决方案4】:

    另一个问题:在

        for(Counter = 0; Counter < CardNum.length; Counter++){
    
            LineNo = CardNum[Counter];
            if(LineNo.contains(CardNumberStr)){
                Exist = true;
                System.out.println("I've found it!");
            }
            else if(Counter == CardNum.length){
                Exist=false;
            }
        }
    

    (Counter == CardNum.length) 永远不会为真,因为 Count 值从 0 变为 CardNum.length-1。由于 Exist 已初始化为 false,因此您无需再次将其设置为 false。你可以去掉 else 子句。

    顺便说一句,你可以跳出循环

        for(Counter = 0; Counter < CardNum.length; Counter++){
            LineNo = CardNum[Counter];
            if(LineNo.contains(CardNumberStr)){
                Exist = true;
                System.out.println("I've found it!");
                break.
            }
        }
    

    【讨论】:

      【解决方案5】:

      您的代码在这一行有误:

      while(Exist = false || line != null);
      

      一定是:

      while(Exist == false || line != null);
                  ^^^^
      

      在您的版本中,您将 false 分配给 Exist 并且您不进行比较。

      【讨论】:

        【解决方案6】:

        您没有根据值 false 评估 Exist,而是将值 false 分配给变量。奇怪的是,条件或那里没有更多错误,但您可以通过将行设置为来修复它

              while(Exist == false || line != null);
        

        我也可能是错的,因为它已经很晚了,我在 iPad 上,但是那个“当”在正确的水平上吗?它可能需要一个大括号。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-17
          • 2016-07-27
          • 2014-05-06
          • 1970-01-01
          • 2013-03-29
          • 1970-01-01
          相关资源
          最近更新 更多