三元运算符


三元运算符

package operator;

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

        a+=b; //a = a+b
        System.out.println(a);

        a-=b; //a = a-b
        System.out.println(a);

        //字符串连接符   + , string
        System.out.println(""+a+b);
        System.out.println(a+b+"");
    }
}


package operator;

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

        int score = 50;
        String type = score <60 ? "不及格":"及格";//必须掌握  //用if判断

        System.out.println(type);

        int score1 = 70;
        String type1 = score1 <60 ? "不及格":"及格";//必须掌握  //用if判断

        System.out.println(type1);
    }
}

相关