1 /*
2 三个和尚
3 需求:
4 一个寺庙里住着三个和尚,他们的身高必须经过车辆得出,请用程序实现获取这三个和尚的最高身高
5 */
6 import java.util.Scanner;
7
8 public class OperatorDemo{
9 public static void main (String[] args){
10 //身高未知,采用键盘录入实现。首先导包,然后创建对象。
11 Scanner sc = new Scanner(System.in);
12
13 //键盘录入三个身高分别赋值给三个变量。
14 System.out.println("请输入第一个和尚的身高(cm):");
15 int height1 = sc.nextInt();
16
17 System.out.println("请输入第二个和尚的身高(cm):");
18 int height2 = sc.nextInt();
19
20 System.out.println("请输入第三个和尚的身高(cm):");
21 int height3 = sc.nextInt();
22
23 //用三元运算符获取前两个和尚的较高身高,并用临时身高变量保存起来。
24
25 int tempHeihgt = height1>height2? height1 : height2;
26
27 //用三元运算符获取临时身高值和第三个和尚的较高身高,并用最大身高变量保存起来。
28 int maxHeight = tempHeihgt>height3? tempHeihgt : height3;
29
30 //输出结果
31 System.out.println("这三个和尚身高中最高的是:"+maxHeight+"cm");
32 }
33 }