公共类 oyws { | |
public static void main(String[] args) { | |
定义两个变量 | |
int a=10; | |
int b=20; | |
获取两个数据中的较大值 | |
int max= a > b ?a : b; | |
输出较大值 | |
System.out.println("max:"+max); | |
} | |
} | |
两只老虎三元简例 | |
公共类 oyws { | |
public static void main(String[] args) { | |
定义两个老虎的体重 | |
整数重量1=180; | |
int weight2=200; | |
用三元运算符实现两个老虎体重的比较,相同返回true,反则是false | |
布尔 b= 权重1== 权重 2?真:假; | |
结果为false | |
System.out.println("b:"+b); | |
} | |
} | |
三个和尚案例 | |
公共类 oyws { | |
public static void main(String[] args) { | |
定义三个和尚身高; | |
英寸高度1=150; | |
int 高度2=210; | |
int 高度3=165; | |
先比前两个身高,高的和下一组比; | |
int tempHeight= height1>height2 ?高度1 :高度2; | |
上一组比完选最高的; | |
int maxHeight=tempHeight>height3 ?tempHeight:height3; | |
选出追高的 | |
System.out.println("maxHeight:"+maxHeight); | |
} | |
} |
【无标题】三元运算符及简单例子
最新推荐文章于 2023-12-08 12:44:22 发布