Appearance
6.4 if…else if…else 语句
if…else if…else 语句的概念
if…else if…else 语句是 if…else 语句的扩展,它用于处理多个条件的情况。当有多个条件需要判断时,可以使用 if…else if…else 语句来实现多分支选择。
if…else if…else 语句的语法
java
if (条件表达式1) {
// 条件1为 true 时执行的代码
} else if (条件表达式2) {
// 条件2为 true 时执行的代码
} else if (条件表达式3) {
// 条件3为 true 时执行的代码
} else {
// 所有条件都为 false 时执行的代码
}- 条件表达式:返回布尔值的表达式
- 代码块:当对应条件为 true 时执行的代码
- else 代码块:当所有条件都为 false 时执行的代码(可选)
if…else if…else 语句的使用
基本用法
java
public class IfElseIfExample {
public static void main(String[] args) {
int score = 85;
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else if (score >= 70) {
System.out.println("Grade: C");
} else if (score >= 60) {
System.out.println("Grade: D");
} else {
System.out.println("Grade: F");
}
}
}示例:根据月份显示季节
java
public class SeasonExample {
public static void main(String[] args) {
int month = 3;
if (month >= 3 && month <= 5) {
System.out.println("Spring");
} else if (month >= 6 && month <= 8) {
System.out.println("Summer");
} else if (month >= 9 && month <= 11) {
System.out.println("Autumn");
} else if (month == 12 || month == 1 || month == 2) {
System.out.println("Winter");
} else {
System.out.println("Invalid month");
}
}
}示例:根据年龄阶段分类
java
public class AgeCategoryExample {
public static void main(String[] args) {
int age = 25;
if (age < 0) {
System.out.println("Invalid age");
} else if (age < 13) {
System.out.println("Child");
} else if (age < 18) {
System.out.println("Teenager");
} else if (age < 65) {
System.out.println("Adult");
} else {
System.out.println("Senior");
}
}
}if…else if…else 语句的嵌套
if…else if…else 语句可以嵌套使用,即在一个代码块中包含另一个 if…else if…else 语句。
java
public class NestedIfElseIfExample {
public static void main(String[] args) {
int age = 20;
boolean hasLicense = true;
if (age >= 18) {
System.out.println("You are an adult.");
if (hasLicense) {
System.out.println("You can drive.");
String carType = "sedan";
if (carType.equals("sedan")) {
System.out.println("You drive a sedan.");
} else if (carType.equals("suv")) {
System.out.println("You drive an SUV.");
} else {
System.out.println("You drive another type of car.");
}
} else {
System.out.println("You need a license to drive.");
}
} else {
System.out.println("You are a minor.");
}
}
}if…else if…else 语句的应用场景
1. 多条件分支
java
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C';
} else {
grade = 'D';
}2. 范围检查
java
if (age < 18) {
System.out.println("Minor");
} else if (age < 65) {
System.out.println("Adult");
} else {
System.out.println("Senior");
}3. 菜单选择
java
if (choice == 1) {
// 执行选项 1
} else if (choice == 2) {
// 执行选项 2
} else if (choice == 3) {
// 执行选项 3
} else {
// 无效选项
}示例:if…else if…else 语句的使用
java
public class IfElseIfApplication {
public static void main(String[] args) {
// 检查成绩等级
int score = 75;
if (score >= 90) {
System.out.println("Excellent!");
} else if (score >= 80) {
System.out.println("Good!");
} else if (score >= 70) {
System.out.println("Average!");
} else if (score >= 60) {
System.out.println("Pass!");
} else {
System.out.println("Fail!");
}
// 检查天气状况
String weather = "rainy";
if (weather.equals("sunny")) {
System.out.println("It's a sunny day.");
} else if (weather.equals("cloudy")) {
System.out.println("It's a cloudy day.");
} else if (weather.equals("rainy")) {
System.out.println("It's a rainy day.");
} else if (weather.equals("snowy")) {
System.out.println("It's a snowy day.");
} else {
System.out.println("Unknown weather.");
}
// 检查数字范围
int number = 50;
if (number < 0) {
System.out.println("Negative number");
} else if (number < 10) {
System.out.println("Single digit");
} else if (number < 100) {
System.out.println("Double digit");
} else if (number < 1000) {
System.out.println("Three digit");
} else {
System.out.println("More than three digits");
}
}
}常见问题
1. 条件顺序错误
症状:条件判断顺序错误,导致某些条件永远不会被执行
解决方案:将更具体的条件放在前面,更 general 的条件放在后面
2. 条件重叠
症状:多个条件可能同时为 true,导致逻辑错误
解决方案:确保条件之间互斥,或者调整条件顺序
3. 代码冗余
症状:多个条件分支中存在重复代码
解决方案:将重复代码提取到条件判断之外,或使用函数封装
4. 嵌套过深
症状:if…else if…else 语句嵌套层次过深,代码可读性差
解决方案:减少嵌套层次,使用逻辑运算符或提取方法
总结
if…else if…else 语句是 if…else 语句的扩展,它用于处理多个条件的情况。通过 if…else if…else 语句,可以根据不同的条件执行不同的代码,实现多分支选择。
在使用 if…else if…else 语句时,需要注意:
- 条件顺序:将更具体的条件放在前面
- 条件互斥:确保条件之间互斥,避免逻辑错误
- 代码可读性:避免嵌套过深,保持代码清晰
- 代码复用:提取重复代码,提高代码可维护性
通过合理使用 if…else if…else 语句,可以使程序根据不同的条件执行不同的代码,实现复杂的业务逻辑。
