三元运算符练习
两只老虎
程序要求:动物园里有两只老虎,体重分别为通过键盘录入获得,
请用程序实现判断两只老虎的体重是否相同。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package hello;
import java.util.Scanner;
public class text3 { public static void main(String[] args) { String a="相同"; String b="不相同"; Scanner sc=new Scanner(System.in); System.out.println("第一只老虎的体重:");
int tiger1=sc.nextInt(); System.out.println("第二只老虎的体重:"); int tiger2=sc.nextInt(); System.out.println(tiger1==tiger2?a:b); } }
|
输出最高和尚的身高
程序要求:
一个神庙里面有三个和尚,请用程序实现获取这三个和尚的最高身高。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package hello;
import java.util.Scanner;
public class texe4 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("输入第一个和尚身高:"); int heshang1= sc.nextInt(); System.out.println("输入第二个和尚身高:"); int heshang2= sc.nextInt(); System.out.println("输入第三个和尚身高:"); int heshang3= sc.nextInt(); int high=(heshang1>heshang2?heshang1:heshang2)>heshang3?(heshang1>heshang2?heshang1:heshang2):heshang3; System.out.println(high); } }
|
顺带一提优先级
![image-20230529232533316]()