三元运算符

/*
三元运算符

格式:
    关系表达式?表达式1:表达式2;
范例:
    a>b?a:b;

执行流程:
    首先计算关系表达式的值
    如果值为ture,表达式1的值就是运算结果
    如果值为false,表达式2的值就是运算结果
*/
public class OperatorDemo {
public static void main(String[] args) {
    //定义两个变量
    int a = 10;
    int b = 20;
    
    //获取两个数据中的较大值
    int max =  a > b ? a : b;
    
    //输出结果
    System.out.println("max:" + max);
}
    }

案例-两只老虎

/*
两只老虎

需求:
    动物园里有两,已知两只老虎的体重分别为100kg,200kg,
    请用程序实现判断两的体重是否相同。
*/
public class OperatorTest01 {
public static void main(String[] args) {
    //1.定义两变量用于保存老虎的体重,单位为kg,这里仅仅体现数值即可。
    int weight1 = 180;
    int weight2 = 200;
    //2.用三元运算符实现老虎体重的判断,体重相同,返回ture,否则,返回false。
    boolean b = weight1 == weight2 ? true : false;
    //3.输出结果
    System.out.println("b:" + b);
}
    }

案例-三个和尚

/*
三个和尚

需求:
    一座寺庙里住着三个和尚,已知他们的身高分别为150cm,210cm,165cm,
    请用程序实现获取这三个和尚的最高身高。
*/
public class OperatorTest02 {
public static void main(String[] args) {
    //1:定义三个变量用于保存和尚的身高,单位为cm,这里仅仅体现数值即可。
    int height1 = 150;
    int height2 = 210;
    int height3 = 165;
    
    //2.用三元运算符获取前两和尚的较高身高值,并用临时身高变量保存起来。
    int tempHheight = height1 > height2 ? height1 : height2;
    
    //3.用三元运算符取临时身高值和第三个和尚较高值,并用最大变量保存。
    int maxHeight = tempHheight > height3 ? tempHheight : height3;
    
    //4.输出结果
    System.out.println("maxHeight" + maxHeight);
}
    }
Last modification:March 26th, 2020 at 10:02 pm
如果觉得我的文章对你有用,请随意赞赏