三元运算符及小结

面试题

package operator;

/**
 * @author WZT
 * @create 2021-03-24 10:51
 */
public class Demo07 {
    
    
    public static void main(String[] args) {
    
    
        int a = 10;
        int b = 20;

        a+=b;//a = a+b

        System.out.println(a);

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

输出

30
50
3020
50

Process finished with exit code 0

三元运算符

package operator;

/**
 * @author WZT
 * @create 2021-03-24 10:55
 */
public class Demo08 {
    
    
    public static void main(String[] args) {
    
    
        //x ? y : z
        //如果x==true,则结果为y,否则为z

        int score = 80;
        String type = score <60 ?"不及格":"及格";
        System.out.println(type);
    }
}

输出

及格

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/weixin_45809838/article/details/115166769