Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java compareTo:如何返回布尔值?_Java - Fatal编程技术网

Java compareTo:如何返回布尔值?

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

我被告知compareTo必须返回int…而不是布尔值。
例如:

返回

如果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;

Yes
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()))

   }   

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

   }