首页 > 其他 > 详细

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

时间:2020-01-17 02:19:43      阅读:162      评论:0      收藏:0      [点我收藏+]
 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);

    }
}

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

原文:https://www.cnblogs.com/xianghaoran/p/12203753.html

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