[上]JAVA学习系列模块三第二章73.运算符_三元运算符练习
视频
[vbilibili]【尚硅谷2024最新JAVA入门视频教程(上部)JAVA零基础入门教程】 https://www.bilibili.com/video/BV1YT4y1H7YM/?p=73&share_source=copy_web&vd_source=85f561e7442caa320f4a23b57edee129[/vbilibili]
笔记
练习1
需求:小明考完试了,判断小明的分数是否及格,返回结果
public class Demo01Ternary {
public static void main(String[] args) {
//定义一个变量,表示小明的分数
int score = 60;
String result = score>=60?"及格":"不及格";
System.out.println("result = " + result);
}
}
练习2
有两个老人,年龄分别为70 80 求出两个老人的最高年龄
public class Demo02Ternary {
public static void main(String[] args) {
int old1 = 70;
int old2 = 80;
int max = old1>old2?old1:old2;
System.out.println("max = " + max);
}
}
练习3
有三个老人,年龄分别为70 80 60 求出三个老人的最高年龄
public class Demo03Ternary {
public static void main(String[] args) {
int old1 = 70;
int old2 = 80;
int old3 = 60;
int temp = old1>old2?old1:old2;
int max = temp>old3?temp:old3;
System.out.println("max = " + max);
}
}
