• Java基础-赋值运算符Assignment Operators与条件运算符Condition Operators


          Java基础-赋值运算符Assignment Operators与条件运算符Condition Operators

                                      作者:尹正杰

    版权声明:原创作品,谢绝转载!否则将追究法律责任。

    一.赋值运算符

      表达式的数据类型要与左边变量的类型兼容

    1>.常规赋值

     1 /*
     2 @author :yinzhengjie
     3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
     4 EMAIL:y1053419035@qq.com
     5 */
     6 
     7 public class Assignment{
     8     public static void main(String[] args){
     9         //1>.赋值
    10         int x = 123;
    11         
    12         x = 123 + 5;
    13         
    14         int y = x / 2;
    15         
    16         // int z = 3.1415926;    //类型不兼容。    
    17         
    18         System.out.println(x);        //128
    19         System.out.println(y);        //64
    20     }
    21 }

    2>.符合赋值,自反赋值

     1 /*
     2 @author :yinzhengjie
     3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
     4 EMAIL:y1053419035@qq.com
     5 */
     6 
     7 public class Assignment2{
     8     public static void main(String[] args){
     9         //复合赋值隐含着强类型转换
    10         
    11         byte  a = 10;
    12         
    13         a += 5;            //相当于 a = (byte)(a + 5)
    14         
    15         System.out.println(a);        //15
    16 }
    17 }

    二.条件运算符

       条件运算符也叫三元运算符。语法格式:“(条件)?表达式1:表达式2”,如果条件成立,整个表达式的值就是表达式1的值,如果条件不成立,整个表达式的值就是表达式2的值。

     1 /*
     2 @author :yinzhengjie
     3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
     4 EMAIL:y1053419035@qq.com
     5 */
     6 
     7 public class Demo{
     8     public static void main(String[] args){
     9         
    10         int a = 10;
    11         int b = 20;
    12         int result = a > b ? a:b;
    13         
    14         /**
    15             如果a > b 成立,就把a的值赋值给变量result;
    16             如果a > b不成立,就把b的值赋值给变量result;
    17             就是把a和b中较大的保存到变量result中。
    18         */
    19         
    20         System.out.println( result );
    21         
    22         String str = a > b ? "a较大":"b较大";
    23         System.out.println( str );
    24     
    25     
    26         int x = 100;
    27         int y = 20;
    28         int z = 50;
    29         
    30         // int max = (x>y?x:y)>z?(x>y?x:y):z;    //不建议这样玩,可以用来跟小白装逼用,哈哈~但是可读性太差。
    31         
    32         int maxAB = x > y ? x:y;
    33         
    34         int max = maxAB > z ? maxAB:z;
    35         
    36         System.out.println(max);
    37         
    38         
    39     }
    40 }

       如果让你比较三个数字的大小,并从键盘输入的咋办呢?这个时候我们就得导入一个类啦,来帮助我们解决这个问题。

     1 /*
     2 @author :yinzhengjie
     3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
     4 EMAIL:y1053419035@qq.com
     5 */
     6 
     7 
     8 import java.util.Scanner;
     9 
    10 public class compare {
    11     public static void main(String[] args){
    12         /**
    13             从键盘输入两个数,显示其中的最大值,要求使用if-else结构.
    14         */
    15         Scanner Input = new Scanner(System.in);
    16         
    17         System.out.print("请输入第一个数字:>>>");
    18         int num1 = Input.nextInt();
    19         
    20         System.out.print("请输入第二个数字:>>>");
    21         int num2 = Input.nextInt();
    22         
    23         System.out.print("请输入第三个数字:>>>");
    24         int num3 = Input.nextInt();
    25     
    26         //方案一:
    27         // if(num1 > num2){
    28             // if (num1 > num3){
    29                 // System.out.println(num1);
    30             // }else{
    31                 // System.out.println(num3);
    32             // }
    33             
    34         // }else{
    35             // if (num2 > num3){
    36                 // System.out.println(num2);
    37             // }else{
    38                 // System.out.println(num3);
    39             // }
    40         // }
    41         
    42         
    43         //方案二:(推荐使用)
    44         int res = (num1 > num2)?num1:num2;
    45         int max = (res > num3)?res:num3;
    46         System.out.println(max);
    47         
    48         
    49         //方案三:(不推荐使用,no 作 no die)
    50         
    51         // System.out.print("请输入第一个数字:>>>");
    52         // int a = Input.nextInt();
    53         
    54         // System.out.print("请输入第二个数字:>>>");
    55         // int b = Input.nextInt();
    56         
    57         // System.out.print("请输入第三个数字:>>>");
    58         // int c = Input.nextInt();
    59         // System.out.println("最大值是:" + ((a > b)?(a>c?a:c):(b>c?b:c)));
    60         
    61     }
    62 }
  • 相关阅读:
    dmesg
    [转]df命令
    [转]linux /proc/cpuinfo 文件分析
    awk
    sed
    [转]进程间通信
    Bootstrap 树形列表与右键菜单
    Maven国内仓库
    《深入剖析Tomcat》源码
    Spring in Action学习笔记(2)
  • 原文地址:https://www.cnblogs.com/yinzhengjie/p/8688553.html
Copyright © 2020-2023  润新知