2014-08-31 109 views
1

我有一個循環的同時,應通過數組元素閱讀,試圖找到指定的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

回答

6

因爲你在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

1

你的代碼是在這一行wron:

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

它必須是:

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

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

2

存在= FALSE這裏是一切罪惡的根源。 =是賦值運算符和==是相等比較運算符。

1

您還沒有評估針對存在的價值假的,要指定值false的變量。很奇怪的是沒有更多的與條件,或有會錯,但你可以通過設置線解決它

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

我也可能是錯的,因爲它是晚,我在一臺iPad,但是這個「同時」是否在正確的水平呢?它可能需要成爲一個大括號。

+0

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

2

你不會在你的循環中重新讀取你的line變量,所以line != null總是如此。

+0

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

2

另一個問題:在

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. 
     } 
    } 
相關問題