关于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。还有其他情况么?

阅读 2.8k
2 个回答

编译器没有人的智慧!
这样写:

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

要保证分支全,这样编译器才认为所有的分支都被处理了!呵呵。

因为 你没有 else

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题