关于操作运算符的一些小练习

 1 public class operatorTest05 {
 2     //猜猜z的值是?
 3     public static void main(String[] args) {
 4         boolean x = true;
 5         boolean y = false;
 6         short z = 40;
 7         if ((z++ == 40) && (y = true)) {//左边为true,z变为41;
 8             //然后右边y值为true,所以执行if语句
 9             z++;//z变为42
10         }
11         if ((x = false) || (++z == 43)) {//左边为false,看右边
12             //右边先自加在取值,z=43,为true,执行if语句
13             z++;//z自加得44
14         }
15         System.out.println("z=" + z);
16 
17     }
18 }
//用三元运算符比较三个数的大小
public class ternaryOperator04 {
    public static void main(String[] args) {
        int n1 = 12;
        int n2 = 31;
        int n3 = -11;
       /* int max1 = (n1 > n2) ? n1 : n2;
        int max2 = (max1 > n3) ? max1 : n3;*/
        int max2 = (((n1 > n2) ? n1 : n2)> n3) ? ((n1 > n2) ? n1 : n2): n3;
        //但是不建议这样书写,降低了可读性
        System.out.println("最大的数是" + max2);

    }
}
posted @ 2020-01-16 23:28  Goldexperience  阅读(193)  评论(0编辑  收藏  举报