首页 > 其他 > 详细

28 三元运算符之三个和尚

时间:2020-05-30 11:08:05      阅读:47      评论:0      收藏:0      [点我收藏+]
 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 }

 

28 三元运算符之三个和尚

原文:https://www.cnblogs.com/zhengqiangchen/p/12990906.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!