Java compareTo:如何返回布尔值?
我被告知compareTo必须返回int…而不是布尔值。Java compareTo:如何返回布尔值?,java,Java,我被告知compareTo必须返回int…而不是布尔值。 例如: 返回 如果a等于b则为0 -1如果ab 我对此有点困惑。任何帮助都将不胜感激 public int compareTo(Cheese anotherCheese) throws ClassCastException { if (!(anotherCheese instanceof Cheese)) throw new ClassCastException("A Cheese obj
例如:
返回 如果a等于b
则为0 -1如果a +1如果a>b
我对此有点困惑。任何帮助都将不胜感激
public int compareTo(Cheese anotherCheese)
throws ClassCastException
{
if (!(anotherCheese instanceof Cheese))
throw new ClassCastException("A Cheese object expected.");
if(getCheeseType().compareTo(anotherCheese.getCheeseType()))
return -1;
else if (getCheeseType().compareTo(anotherCheese.getCheeseType()))
return 1;
else
return cheesePrice > anotherCheese.getCheesePrice();
}
当我编译时,会收到错误消息,上面写着:
不兼容类型
if(getCheeseType().compareTo(另一个cheese.getCheeseType())
不兼容类型
else if(getCheeseType().compareTo(另一个cheese.getCheeseType())
不兼容类型
return cheesePrice>anotherCheese.getCheesePrice()
compareTo
确实返回int
,而不是布尔值。因此会出现编译错误。在if
语句中,必须放置布尔值
所以不是
if(getCheeseType().compareTo(另一个cheese.getCheeseType())
写
if(getCheeseType().compareTo(另一个cheese.getCheeseType())<0)
compareTo
确实返回一个int
,而不是布尔值。因此会出现编译错误。在if
语句中,必须放置布尔值
所以不是
if(getCheeseType().compareTo(另一个cheese.getCheeseType())
写
if(getCheeseType().compareTo(另一个cheese.getCheeseType())<0)
只要做
return getCheeseType().compareTo(anotherCheese.getCheeseType());
如果你还想比较价格,那就做吧
if(getCheeseType().compareTo(anotherCheese.getCheeseType())!=0)
return getCheeseType().compareTo(anotherCheese.getCheeseType());
//cheeseTypes are equal
if(cheesePrice < anotherCheese.getCheesePrice())
return -1;
else if (cheesePrice > anotherCheese.getCheesePrice())
return 1;
else
return 0;
if(getCheeseType().compareTo(另一个cheese.getCheeseType())!=0)
返回getCheeseType().compareTo(另一个cheese.getCheeseType());
//奶酪类型是相同的
if(cheesePriceanotherCheese.getCheesePrice())
返回1;
其他的
返回0;
就这么做吧
return getCheeseType().compareTo(anotherCheese.getCheeseType());
如果你还想比较价格,那就做吧
if(getCheeseType().compareTo(anotherCheese.getCheeseType())!=0)
return getCheeseType().compareTo(anotherCheese.getCheeseType());
//cheeseTypes are equal
if(cheesePrice < anotherCheese.getCheesePrice())
return -1;
else if (cheesePrice > anotherCheese.getCheesePrice())
return 1;
else
return 0;
if(getCheeseType().compareTo(另一个cheese.getCheeseType())!=0)
返回getCheeseType().compareTo(另一个cheese.getCheeseType());
//奶酪类型是相同的
if(cheesePriceanotherCheese.getCheesePrice())
返回1;
其他的
返回0;
//按类型排序。
int delta=getCheeseType().compareTo(另一个cheese.getCheeseType());
如果(delta!=0){返回delta;}
//如果类型相同,请按价格订购。
if(cheesePriceanotherCheese.getCheesePrice()){return 1;}
返回0;
//按类型排序。
int delta=getCheeseType().compareTo(另一个cheese.getCheeseType());
如果(delta!=0){返回delta;}
//如果类型相同,请按价格订购。
if(cheesePriceanotherCheese.getCheesePrice()){return 1;}
返回0;
YescompareTo
方法将返回int类型,而不是布尔值。但是您正在if循环中使用getCheeseType().compareTo(另一个cheese.getCheeseType())
,这将导致编译时错误。像这样改变
public int compareTo(Cheese anotherCheese)
throws ClassCastException
{
if (!(anotherCheese instanceof Cheese))
throw new ClassCastException("A Cheese object expected.");
else return getCheeseType().compareTo(anotherCheese.getCheeseType()))
}
是compareTo
方法将返回int类型,而不是布尔值。但是您正在if循环中使用getCheeseType().compareTo(另一个cheese.getCheeseType())
,这将导致编译时错误。像这样改变
public int compareTo(Cheese anotherCheese)
throws ClassCastException
{
if (!(anotherCheese instanceof Cheese))
throw new ClassCastException("A Cheese object expected.");
else return getCheeseType().compareTo(anotherCheese.getCheeseType()))
}