首页 > 移动平台 > 详细

Java错误笔记:Operator cannot be applied to boolean,int

时间:2020-04-03 23:23:14      阅读:872      评论:0      收藏:0      [点我收藏+]
今天在学习 if 语句时写下了这样一段代码:

        if( 3<= i<= 5 ){
            System.out.println("春天");
        }else if( 6 <= i  <= 8 ){
            System.out.println("夏天");
        }else if( 9 <=i  <=11 ){
            System.out.println("秋天");
        }else{
            System.out.println("冬天");
        };

程序报错:Operator ‘<=‘ cannot be applied to ‘boolean‘,‘int‘

原因是Java中 if 语句不支持这样的表达方式。正确的表达方式应该为

             if( 3<= i && i <= 5 ){
            System.out.println("春天");
        }else if( 6 <= i && i <= 8 ){
            System.out.println("夏天");
        }else if( 9 <=i && i <=11 ){
            System.out.println("秋天");
        }else{
            System.out.println("冬天");
        };

代码的书写方式应该符合规范,理应注意。

Java错误笔记:Operator cannot be applied to boolean,int

原文:https://blog.51cto.com/14774208/2484815

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!