package study;
//三个和尚身高分别为210cm、165cm、150cm,比较他们的身高,并找出最高的和尚
public class eg1 {
public static void main(String[] args) {
//1,定义三个变量用于保存和尚的身高
int height1=210;
int height2=165;
int height3=150;
//2、用三元运算符获取前两个和尚的较高身高,并用临时变量保存起来
int tempheight=(height1>height2)?height1:height2;
System.out.println("tempheight:"+tempheight);
//3、用三元运算符获取临时值和第三个和尚身高较高值,并用最大身高保存起来
int max=(tempheight>height3)?tempheight:height3;
//输出结果
System.out.println("max:"+max);
}
}
第二种,需要我们自己从键盘录入三个和尚的身高
package study;
//三个和尚案列。和尚身高未知
/*需求:
* 一座寺庙里住着三个和尚,他们的身高必须经过测量得出,请用程序获取三个和尚的身高*/
//导包
import java.util.Scanner;
public class eg2 {
public static void main(String[] args) {
//导包
Scanner sc = new Scanner(System.in);
//键盘输入三个身高分别赋值给三个变量
System.out.println("请输入第一个和尚的身高:");
int height1=sc.nextInt();
System.out.println("请输入第二个和尚的身高:");
int height2=sc.nextInt();
System.out.println("请输入第三个和尚的身高:");
int height3=sc.nextInt();
/*//分别输出三个和尚的身高
System.out.println("height1:"+height1);
System.out.println("height2:"+height2);
System.out.println("height3:"+height3);*/
/*三元运算符获取临时身高和第三个和尚身高比较*/
int tempheight=(height1>height2)?height1:height2;
int maxheight=(tempheight>height3)?tempheight:height3;
System.out.println("身高最高的是:"+maxheight +"cm");
}
}