本文目录导读:

我来为你详细介绍Java中if-else的各种实现案例:
基本if-else结构
最简单的if
int score = 85;
if (score >= 60) {
System.out.println("及格了");
}
if-else
int age = 18;
if (age >= 18) {
System.out.println("成年人");
} else {
System.out.println("未成年人");
}
多重if-else if
int score = 85;
if (score >= 90) {
System.out.println("优秀");
} else if (score >= 80) {
System.out.println("良好");
} else if (score >= 70) {
System.out.println("中等");
} else if (score >= 60) {
System.out.println("及格");
} else {
System.out.println("不及格");
}
实际应用案例
案例1:判断闰年
public class LeapYearCheck {
public static void main(String[] args) {
int year = 2024;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.println(year + "年是闰年");
} else {
System.out.println(year + "年不是闰年");
}
}
}
案例2:计算个人所得税
public class TaxCalculator {
public static void main(String[] args) {
double salary = 15000;
double tax = 0;
if (salary <= 5000) {
tax = 0;
} else if (salary <= 8000) {
tax = (salary - 5000) * 0.03;
} else if (salary <= 17000) {
tax = (salary - 5000) * 0.1 - 210;
} else if (salary <= 30000) {
tax = (salary - 5000) * 0.2 - 1410;
} else {
tax = (salary - 5000) * 0.25 - 2660;
}
System.out.println("应缴个人所得税:" + tax + "元");
}
}
案例3:登录验证
public class LoginSystem {
public static void main(String[] args) {
String username = "admin";
String password = "123456";
String inputUsername = "admin";
String inputPassword = "123456";
if (inputUsername.equals(username)) {
if (inputPassword.equals(password)) {
System.out.println("登录成功!");
} else {
System.out.println("密码错误!");
}
} else {
System.out.println("用户名不存在!");
}
}
}
案例4:商品折扣系统
public class DiscountSystem {
public static void main(String[] args) {
double price = 200;
int quantity = 5;
double totalPrice;
double discount = 0;
totalPrice = price * quantity;
// 根据数量和金额计算折扣
if (quantity >= 10) {
discount = 0.2; // 20%折扣
} else if (quantity >= 5) {
discount = 0.1; // 10%折扣
} else if (totalPrice >= 1000) {
discount = 0.15; // 15%折扣
} else if (totalPrice >= 500) {
discount = 0.05; // 5%折扣
} else {
discount = 0; // 无折扣
}
double finalPrice = totalPrice * (1 - discount);
System.out.println("原价:" + totalPrice + "元");
System.out.println("折扣:" + (discount * 100) + "%");
System.out.println("最终价格:" + finalPrice + "元");
}
}
条件运算符(三元运算符)
// 传统if-else
int age = 20;
String status;
if (age >= 18) {
status = "成年";
} else {
status = "未成年";
}
// 使用三元运算符
String status2 = (age >= 18) ? "成年" : "未成年";
嵌套if-else的优化
不好的写法
if (condition1) {
if (condition2) {
// 执行代码
}
}
优化写法
if (condition1 && condition2) {
// 执行代码
}
常见错误和注意事项
// 错误1:使用=而不是==
int num = 10;
// if (num = 5) { // 错误,应该用==
if (num == 5) {
System.out.println("等于5");
}
// 错误2:忘记大括号
if (num > 0)
System.out.println("正数");
System.out.println("这条语句总是执行"); // 不在if范围内
// 正确做法
if (num > 0) {
System.out.println("正数");
System.out.println("这也是if范围内的");
}
实用示例:成绩等级评定的完整类
import java.util.Scanner;
public class GradeEvaluator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入学生成绩(0-100):");
int score = scanner.nextInt();
// 检查输入有效性
if (score < 0 || score > 100) {
System.out.println("成绩必须在0-100之间");
} else {
String grade;
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else if (score >= 70) {
grade = "C";
} else if (score >= 60) {
grade = "D";
} else {
grade = "F";
}
System.out.println("成绩等级:" + grade);
// 附加评语
if (grade.equals("A")) {
System.out.println("优秀!继续保持!");
} else if (grade.equals("F")) {
System.out.println("不及格,需要努力!");
}
}
scanner.close();
}
}
这些案例覆盖了if-else的主要使用场景,关键点是:
- 逻辑条件要清晰
- 注意代码缩进和可读性
- 避免过深嵌套
- 记得处理边界情况