gpt4 book ai didi

java - .equals() 和 == 的问题

转载 作者:行者123 更新时间:2023-12-01 07:32:26 25 4
gpt4 key购买 nike

我在使用这些 if 语句时遇到问题,无法找出问题所在:

  1. 如果我尝试将它们更改为嵌套的 if, else if 格式,我会收到编译器错误,提示“else”,但没有 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

最佳答案

删除带有 if 的行末尾的 ;

关于java - .equals() 和 == 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16244554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com