为什么这个while循环执行无限次?

问题描述:

我有一个循环的同时,应通过数组元素阅读,试图找到指定的variable的价值,然而,循环执行本身无限次,我不明白为什么。一旦它找到了它正在寻找的价值,它应该退出;我知道它确实查找在寻找,因为它打印出来的方法I've found it!无限倍。代码,到目前为止是:为什么这个while循环执行无限次?

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

谁能帮助我找出它为什么这样做吗?

+1

,我建议你设置的Eclipse(或任何IDE使用),当你有一个布尔值分配在一个if /而块提醒你的了。 – corsiKa 2014-08-31 07:53:40

+1

它可能是while(Exist == false ...而不是Exist = false ???? – mlwn 2014-08-31 07:53:44

因为你在do-while循环分配Exist = false。它应该是Exists == false或更好的方式:!Exist

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

除了这一点,请按照Java Code Conventions(旧但仍在使用),其中的变量应该与骆驼情况下宣告但小写字母开始。


查看更多你的代码,你永远读你的文件的另一行和逻辑为您do-while应使用AND(&&),而不是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); 
+0

我改变了它,它仍然通过无限次循环? – James 2014-08-31 07:57:25

+0

@James答案更新。 – 2014-08-31 08:00:35

+0

太棒了!谢谢! – James 2014-08-31 08:05:53

你的代码是在这一行wron:

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

它必须是:

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

在你的版本分配falseExist和你不要比较。

存在= FALSE这里是一切罪恶的根源。 =是赋值运算符和==是相等比较运算符。

您还没有评估针对存在的价值假的,要指定值false的变量。很奇怪的是没有更多的与条件,或有会错,但你可以通过设置线解决它

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

我也可能是错的,因为它是晚,我在一台iPad,但是这个“同时”是否在正确的水平呢?它可能需要成为一个大括号。

+0

不,它在正确的水平上,我错了。 – 2014-08-31 08:09:48

你不会在你的循环中重新读取你的line变量,所以line != null总是如此。

+0

+1,很好的捕捉,甚至滑倒我的眼睛。无法看到,一旦通过代码粘贴:-) – 2014-08-31 08:06:07

另一个问题:在

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

(计数器== CardNum.length)永远不会为真,由于计数值从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. 
     } 
    }