java的三元运算符

三元运算符

  • 格式为:X ? Y : Z
  • 含义:如果x==Ture,则结果输出为Y,否则结果输出为Z
  • 虽然可以通过if流程控制来判断,但是这个语句在开发中很常用,其可以使代码更加精简,便于理解。
public class Demo05 {
    
    
    public static void main(String[] args) {
    
    

        //X ? Y : Z(如果x==Ture,则结果输出为Y,否则结果输出为Z)
        int score = 90;
        String a = score <= 60 ? "不及格" : "及格";
        System.out.println(a);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43409668/article/details/112852303