2012-09-16 79 views
2

foreach循環完全忽略我的if語句。for循環忽略布爾如果語句

 for(InfoBox infoBox : mAbilities) 
     { 
      if(infoBox.CheckPressed(event)); 
      { 
       //This is being outputted each time, even if the if statement returns false. 
       System.out.println(infoBox.getName()); 
      } 

      System.out.println(infoBox.CheckPressed(event)); 
      System.out.println(infoBox.getName()); 
     } 
+2

這就是爲什麼你應該尊重[Java Code Conventions](http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html),並將左括號放在同一行,如/ if等。 – Xaerxess

+0

您有一個「系統.out.println(infoBox.getName());」在{}之外的if {}之外。你確定這不是你看到的印刷品嗎?... – TheCodeArtist

回答

11

你提前終止你的if語句都以分號:

if(infoBox.CheckPressed(event)); // <-- remove the semicolon 

這使得下面的塊中的獨立塊,將永遠執行。

+0

謝謝,這樣的傻瓜。 –

2

我想這是你必須做無意中.....

if(infoBox.CheckPressed(event));

刪除semicolon在上面if聲明

2

您應該刪除,如果分號聲明

if(infoBox.CheckPressed(event));