我有if語句與這些麻煩並不能找出問題:如果我嘗試改變他們到嵌套如果麻煩與.equals()和==
- ,否則,如果我的格式得到一個編譯器錯誤,說「其他」沒有if語句。
- 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
而不是'toUpperCase',你可以使用'equalsIgnoreCase'。 – 2013-04-26 20:20:40
我還是不明白你爲什麼要用分號''來關閉'if'語句。 – 2013-04-26 20:21:09