Java学习_002 案例2:三个和尚

发布时间 2023-09-08 21:05:01作者: 一切推倒重来

需求:一座寺庙里住着三个和尚,已知他们的身高分别为150cm、210cm、165cm,请用程序实现获取这三个和尚的最高身高。

 1 public class Main {
 2     public static void main(String[] args) {
 3         int height1 = 150;
 4         int height2 = 210;
 5         int height3 = 165;
 6         //使用三元运算符获取前两个和尚的身高值,并且使用临时变量保存起来
 7         int tempHeight = height1 > height2 ? height1 :height2;
 8 
 9         tempHeight = tempHeight >height3 ? tempHeight : height3;
10         //输出最高身高
11         System.out.println("最高身高为:"+ tempHeight);
12         }
13     }

运行结果: