Home  >  Q&A  >  body text

java - 关于int比较的返回值问题

            int result = Integer.compare(1,2);
            if(result > 0){
                return -1;
            }else if(result < 0){
                return 1;
            }else if(result == 0) {
                return 0;
            }

这段代码一直报缺少返回值错误,有点想不明白,int不久三种么? 等于0、大于0、小于0。还有其他情况么?

PHP中文网PHP中文网2741 days ago300

reply all(2)I'll reply

  • 迷茫

    迷茫2017-04-17 17:50:50

    The compiler has no human wisdom!
    Write like this:

                int result = Integer.compare(1,2);
                if(result > 0){
                    return -1;
                }else if(result < 0){
                    return 1;
                }else{
                    return 0;
                }

    Ensure that all branches are complete, so that the compiler will think that all branches have been processed! hehe.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 17:50:50

    Because you don’t have else

    reply
    0
  • Cancelreply