我得到这个代码:输入从nextLine阅读不等于字符串值
System.out.println("Enter the brand and cash value");
String brand = keyboard.nextLine();
long cash = keyboard.nextDouble();
String buffer = keyboard.nextLine();
,但即使我进入我想比较确切的字符串值,它没有认识到它们是相同的。奇怪的是,当我进入这个:
compare[0] = new Car ("BMW", 12.00);
,而不是这样的:
compare[0] = new Car (brand, 12.00);
它的工作原理
我也用等于:
public boolean equals(Car other)
{
if (other == null)
{
return false;
}
if(this.brand == other.brand && this.cash == other.cash)
{
return true;
}
else
{
return false;
}
}
您是否使用.equals()方法检查相等性? ==在这里不起作用 –
在进行比较之前,打印出您正在比较的两个字符串。这应该可以帮助您查看是否有任何差异。另外,不要使用'=='来比较字符串(如前所述)。 –
我使用等于我重新定义了它 –