一、单if语句
条件成立则执行,条件不成立则越过
1、三个不同数的最大值
int a = 10, b = 2, c = -5;
int max = 0;
if (a > b && a > c) {
max = a;
}
if (b > a && b > c) {
max = b;
}
if (c > a && c > b) {
max = c;
}
System.out.println(max); // 10
2、三个不同数从小到大排序
int a = 10, b = 2, c = -5;
int temp = 0;
if (b < a) {
temp = a;
a = b;
b = temp;
}
if (c < a) {
temp = a;
a = c;
c = temp;
}
if (c < b) {
temp = b;
b = c;
c = temp;
}
System.out.println(a + " " + b + " " + c); // -5 2 10
二、标准if-else语句
二者选其一,等效于三元运算符
1、奇数偶数
int number = 12;
if (number % 2 == 0) {
System.out.println("偶数");
} else {
System.out.println("奇数");
}
System.out.println(number % 2 == 0 ? "偶数" : "奇数");
三、扩展if-else语句
(1)if-else if多分支 VS 多重if单分支
如果条件是互斥的,或者在满足某个条件后跳过后续条件的检查,那么使用if-else if
结构
如果条件可能同时为真,或者在每个条件为真时都执行相应的代码,那么使用多个独立的if
语句
(2)if-else if多分支 VS switch-case
n者选其一,switch-case可能表现出更优的性能
if-else if主要用于区间判断,switch-case主要用于定值判断
1、成绩等级
int score = 87;
// 多重if单分支
// if (score >= 90) {
// System.out.println("A");
// }
// if (score >= 80 && score < 90) {
// System.out.println("B");
// }
// if (score >= 70 && score < 80) {
// System.out.println("C");
// }
// if (score >= 60 && score < 70) {
// System.out.println("D");
// }
// if (score < 60) {
// System.out.println("E");
// }
// if-else if 冗杂多分支
// if (score >= 90) {
// System.out.println("A");
// } else if (score >= 80 && score < 90) {
// System.out.println("B");
// } else if (score >= 70 && score < 80) {
// System.out.println("C");
// } else if (score >= 60 && score < 70) {
// System.out.println("D");
// } else {
// System.out.println("E");
// }
// if-else if 简化多分支 从大到小判断
// if (score >= 90) {
// System.out.println("A");
// } else if (score >= 80) { // score < 90 else否定上个条件
// System.out.println("B");
// } else if (score >= 70) { // score < 80 else否定上个条件
// System.out.println("C");
// } else if (score >= 60) { // score < 70 else否定上个条件
// System.out.println("D");
// } else {
// System.out.println("E");
// }
// if-else if 简化多分支 从小到大判断
if (score < 60) {
System.out.println("E");
} else if (score < 70) { // score >= 60 else否定上个条件
System.out.println("D");
} else if (score < 80) { // score >= 70 else否定上个条件
System.out.println("C");
} else if (score < 90) { // score >= 80 else否定上个条件
System.out.println("B");
} else {
System.out.println("A");
}
2、星期判断
int weekday = 8;
// if-else if 多分支
if (weekday == 1) {
System.out.println("星期一");
} else if (weekday == 2) {
System.out.println("星期二");
} else if (weekday == 3) {
System.out.println("星期三");
} else if (weekday == 4) {
System.out.println("星期四");
} else if (weekday == 5) {
System.out.println("星期五");
} else if (weekday == 6) {
System.out.println("星期六");
} else if (weekday == 7) {
System.out.println("星期日");
} else { // else部分可选,检验不合理数据,增强程序健壮性
System.out.println("数据不合理");
}
// switch-case 选择结构
switch (weekday) {
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
default: // default部分可选,检验不合理数据,增强程序健壮性
System.out.println("数据不合理");
break; // default语句体放在最后,该break可以省略,推荐不省略
}
3、判断整除
判断这个数能被3、5、7中的谁整除,能被谁整除就把这个数字输出
- 如果3、5、7都不能整除的,则输出"NO"
- 如果能被多个数字整除,按从小到大的顺序把数字输出,中间用空格隔开
三个条件都为真的反面,是指这三个条件中至少有一个不为真
换句话说,就是这三个条件的逻辑与(&&)的结果为假(false)
A && B && C,反面可以表达为 !(A && B && C),这等价于 !A || !B || !C
int num = 105;
// 多重if单分支
if (num % 3 == 0) {
System.out.print(3 + " ");
}
if (num % 5 == 0) {
System.out.print(5 + " ");
}
if (num % 7 == 0) {
System.out.print(7 + " ");
}
if (num % 3 != 0 && num % 5 != 0 && num % 7 != 0) {
System.out.println("NO");
}
// if-else if多分支
// if (num % 3 == 0 && num % 5 == 0 && num % 7 == 0) { // 三者
// System.out.println(3 + " " + 5 + " " + 7);
// } else if (num % 3 == 0 && num % 5 == 0) { // 两者Begin
// System.out.println(3 + " " + 5);
// } else if (num % 3 == 0 && num % 7 == 0) {
// System.out.println(3 + " " + 7);
// } else if (num % 5 == 0 && num % 7 == 0) {
// System.out.println(5 + " " + 7);
// } else if (num % 3 == 0) { // 一者Begin
// System.out.println(3);
// } else if (num % 5 == 0) {
// System.out.println(5);
// } else if (num % 7 == 0) {
// System.out.println(7);
// } else {
// System.out.println("NO");
// }