简体   繁体   English

compareTo:如何返回布尔值?

[英]compareTo: how to return a boolean?

I am told that compareTo has to return an int...not Boolean. 我被告知compareTo必须返回一个int ...而不是布尔值。
For example: 例如:

Returns 返回

0 if a equal b 0如果等于b
-1 if a < b -1如果a <b
+1 if a > b +1如果a> b

I am slightly confused by that. 我有点困惑。 Any help would be greatly appreciated. 任何帮助将不胜感激。

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();
   }   

When I compile, I get the error messages saying: 当我编译时,我得到错误消息说:


incompatible types 不兼容的类型
if(getCheeseType().compareTo(anotherCheese.getCheeseType()))
incompatible types 不兼容的类型
else if (getCheeseType().compareTo(anotherCheese.getCheeseType()))
incompatible types 不兼容的类型
return cheesePrice > anotherCheese.getCheesePrice();

compareTo does indeed return an int , not a boolean . compareTo确实返回一个int ,而不是一个boolean You get those compile errors because of that. 因此,你得到那些编译错误。 Inside an if statement, you must put a boolean . if语句中,您必须放置一个boolean

So instead of 而不是

if(getCheeseType().compareTo(anotherCheese.getCheeseType()))

write

if(getCheeseType().compareTo(anotherCheese.getCheeseType()) < 0)

just do 做就是了

return getCheeseType().compareTo(anotherCheese.getCheeseType());

if you want to also compare by price then do 如果你想按价格比较那么做

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;
   // Order by type.
   int delta = getCheeseType().compareTo(anotherCheese.getCheeseType());
   if (delta != 0) { return delta; }
   // If of the same type, order by price.
   if (cheesePrice < anotherCheese.getCheesePrice()) { return -1; }
   if (cheesePrice > anotherCheese.getCheesePrice()) { return 1; }
   return 0;

Yes compareTo method will return int type, not a boolean . compareTo方法将返回int类型,而不是boolean But you are using getCheeseType().compareTo(anotherCheese.getCheeseType()) in if loop, which will leads to compile time errors. 但是你在if循环中使用getCheeseType().compareTo(anotherCheese.getCheeseType()) ,这将导致编译时错误。 change like this. 像这样改变。

public int compareTo(Cheese anotherCheese)
   throws ClassCastException
   {
       if (!(anotherCheese instanceof Cheese))
            throw new ClassCastException("A Cheese object expected.");

       else return getCheeseType().compareTo(anotherCheese.getCheeseType()))

   }   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM