Appearance
6.3 if…else 语句
if…else 语句的概念
if…else 语句是 if 语句的扩展,它根据条件的真假来决定执行哪一段代码。当条件为 true 时执行 if 代码块,当条件为 false 时执行 else 代码块。
if…else 语句的语法
java
if (条件表达式) {
// 条件为 true 时执行的代码
} else {
// 条件为 false 时执行的代码
}- 条件表达式:一个返回布尔值的表达式
- if 代码块:当条件表达式为 true 时执行的代码
- else 代码块:当条件表达式为 false 时执行的代码
if…else 语句的使用
基本用法
java
public class IfElseExample {
public static void main(String[] args) {
int age = 16;
if (age >= 18) {
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are not eligible to vote yet.");
}
}
}示例:比较两个数的大小
java
public class CompareNumbersExample {
public static void main(String[] args) {
int a = 10;
int b = 20;
if (a > b) {
System.out.println("a is greater than b");
} else {
System.out.println("a is less than or equal to b");
}
}
}示例:检查字符串是否为空
java
public class CheckStringExample {
public static void main(String[] args) {
String name = "";
if (name != null && !name.isEmpty()) {
System.out.println("Hello, " + name + "!");
} else {
System.out.println("Hello, Guest!");
}
}
}if…else 语句的嵌套
if…else 语句可以嵌套使用,即在 if 或 else 代码块中包含另一个 if…else 语句。
java
public class NestedIfElseExample {
public static void main(String[] args) {
int age = 20;
boolean hasLicense = false;
if (age >= 18) {
System.out.println("You are an adult.");
if (hasLicense) {
System.out.println("You can drive.");
} else {
System.out.println("You need a license to drive.");
}
} else {
System.out.println("You are a minor.");
}
}
}if…else 语句与逻辑运算符的结合
java
public class IfElseWithLogicalOperatorsExample {
public static void main(String[] args) {
int score = 75;
if (score >= 60 && score < 80) {
System.out.println("You passed the exam.");
} else {
System.out.println("You did not pass the exam.");
}
String username = "user";
String password = "password";
if (username.equals("admin") && password.equals("123456")) {
System.out.println("Login successful as admin.");
} else if (username.equals("user") && password.equals("password")) {
System.out.println("Login successful as user.");
} else {
System.out.println("Login failed.");
}
}
}if…else 语句的应用场景
1. 二选一操作
java
if (isLoggedIn) {
showUserDashboard();
} else {
showLoginPage();
}2. 状态判断
java
if (order.isDelivered()) {
System.out.println("Order has been delivered.");
} else {
System.out.println("Order is still processing.");
}3. 数据验证
java
if (email.contains("@")) {
System.out.println("Email is valid.");
} else {
System.out.println("Email is invalid.");
}示例:if…else 语句的使用
java
public class IfElseApplication {
public static void main(String[] args) {
// 检查年龄
int age = 17;
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}
// 检查成绩
int score = 85;
if (score >= 90) {
System.out.println("Grade: A");
} else {
System.out.println("Grade: B or below");
}
// 检查温度
double temperature = 25.5;
if (temperature > 30) {
System.out.println("It's hot outside.");
} else if (temperature > 20) {
System.out.println("It's warm outside.");
} else {
System.out.println("It's cool outside.");
}
// 检查是否为闰年
int year = 2023;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
}常见问题
1. 条件表达式错误
症状:条件表达式语法错误或逻辑错误
解决方案:确保条件表达式返回布尔值,检查逻辑是否正确
2. 代码块缺失
症状:if 或 else 语句后没有使用大括号,导致只有第一行代码被执行
解决方案:始终使用大括号包围 if 和 else 语句的代码块,即使只有一行代码
3. 嵌套过深
症状:if…else 语句嵌套层次过深,代码可读性差
解决方案:减少嵌套层次,使用逻辑运算符或提取方法
总结
if…else 语句是 if 语句的扩展,它根据条件的真假来决定执行哪一段代码。if…else 语句的语法简单明了,但功能强大,可以处理各种二选一的条件判断场景。
在使用 if…else 语句时,需要注意:
- 条件表达式必须返回布尔值
- 始终使用大括号包围代码块
- 避免嵌套过深,保持代码可读性
- 结合逻辑运算符处理复杂条件
通过合理使用 if…else 语句,可以使程序根据不同的条件执行不同的代码,实现复杂的业务逻辑。
