Day16 三元运算符

发布时间 2023-12-03 15:07:29作者: 白小帆

三元运算符

扩展赋值运算符 += -= *= /=

package operator;

public class Demo07 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;

        a+=b;// a=a+b
        a-=b;  //即a=a-b    不推荐比较偷懒

        //字符串连接符         +     ,string
        System.out.println(""+a+b);
        System.out.println(a+b+"");  //如果字符串在前面则 后面的转成字符串拼接    如果字符串在后面  前面的会依旧进行运算

        a*=b;//a=a*b
        System.out.println(a);//结果为200

条件运算符

? : (必须掌握)

package operator;

public class Demo08 {
    public static void main(String[] args) {
        //x ? y : z
        //如果x==true,则结果为y 否则结果为z

        int score = 80;
        String type = score<60 ?"不及格":"及格";// 必须掌握(虽然很偷懒)
        // if
        System.out.println(type);//结果是及格
    }
}