2013-04-26 112 views
0

我有if語句與這些麻煩並不能找出問題:如果我嘗試改變他們到嵌套如果麻煩與.equals()和==

  1. ,否則,如果我的格式得到一個編譯器錯誤,說「其他」沒有if語句。
  2. if語句甚至不起作用,正如您從下面的輸出中看到的那樣,即使字符串不相等,它們也會運行。

任何幫助將是偉大的。謝謝!

if (labelArray[0] == e.getSource() && myAppliance.size() >= 1) { 
    if (myAppliance.get(0).getClass().getName().toUpperCase().equals("CLOCK")); { 
     System.out.println(options[0]); 
     System.out.println(myAppliance.get(0).getClass().getName()); 
     infoBox((myAppliance.get(0).toString()),"Info"); 
    } 
    if (myAppliance.get(0).getClass().getName().toUpperCase().equals("LAMP")); //"Clock", "Lamp", "Television" 
    { 
     System.out.println(options[1]); 
     System.out.println(myAppliance.get(0).getClass().getName()); 
     infoBox((myAppliance.get(0).toString()),"Info"); 
    } 
    if (myAppliance.get(0).getClass().getName().toUpperCase().equals("TELEVISION")); { 
     System.out.println(options[2]); 
     System.out.println(myAppliance.get(0).getClass().getName()); 
     infoBox((myAppliance.get(0).toString()),"Info"); 
    } 
} 

輸出:

Clock 
Clock 
Lamp 
Clock 
Television 
Clock 
+2

而不是'toUpperCase',你可以使用'equalsIgnoreCase'。 – 2013-04-26 20:20:40

+1

我還是不明白你爲什麼要用分號''來關閉'if'語句。 – 2013-04-26 20:21:09

回答

7

在與if S上的線的端部卸下;

0

刪除立竿見影; if語句,編譯器把它們作爲陳述後,它失去了使用if本身的目的。

0

如果它們可以使用下一條語句,那麼它們將在下一行運行,或將大括號中的塊視爲語句。通過在if之後編寫;,它基本上使用空語句,然後繼續執行您的代碼塊。

只要刪除;它應該工作。