2013-04-26 108 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之后编写;,它基本上使用空语句,然后继续执行您的代码块。

只要删除;它应该工作。