条件;
90~100 优秀
80~89 好
70~79良
60~69及格
60以下 不及格
public class Demo05IfElsePractise {
public static void main(String[] args) {
nt score = 120;
if (score >= 90 && score <= 100) {
System.out.println("优秀");
} else if (score >= 80 && score < 90) {
System.out.println("好");
} else if (score >= 70 && score < 80) {
System.out.println("良");
} else if (score >= 60 && score < 70) {
System.out.println("及格");
} else if (score >= 0 && score < 60) {
System.out.println("不及格");
} else { // 单独处理边界之外的不合理情况
System.out.println("数据错误");
}
}
}
使用三元运算符和标准的if-else语句分别实现:取两个数字当中的最大值;
public class Demo06Max {
public static void main(String[] args) {
int a = 105;
int b = 20;
// 首先使用三元运算符
// int max = a > b ? a : b;
-----------------------------------------------------------------
// 使用今天的if语句
int max;
if (a > b) {
max = a;
} else {
max = b;
}
System.out.println("最大值:" + max);
}
}