1 /*
2 三元运算符之三个和尚
3 需求:
4
5 一座寺庙里住着三个和尚,已知他们的身高分别为150cm、210cm、165cm
6 请用程序实现获取这三个和尚的最高身高。
7 */
8
9 public class OperatorDemo{
10 public static void main (String[] args){
11 //1.定义三个变量用于保存和尚的身高,单位为cm,这里仅仅体现数值即可。
12 int height1 = 150;
13 int height2 = 210;
14 int height3 = 165;
15
16 //2.用三元运算符获取前两个和尚的较高身高,并用临时身高变量保存起来。
17 int temHeight = height1 >height2 ? height1 : height2;
18
19 //3.用三元运算符获取临时身高值和第三个和尚较高值,并用最大身高变量保存。
20 int maxHeight = temHeight > height3? temHeight : height3;
21
22 //输出结果
23 System.out.println("maxHeight:"+maxHeight);
24
25 }
26 }