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

今天在学习 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("冬天");
        };

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

猜你喜欢

转载自blog.51cto.com/14774208/2484815