本文目录导读:

我来通过几个循序渐进的实操案例,帮你掌握Java条件判断的使用:
案例1:基础if-else - 判断奇偶数
public class OddEvenCheck {
public static void main(String[] args) {
int number = 15;
if (number % 2 == 0) {
System.out.println(number + " 是偶数");
} else {
System.out.println(number + " 是奇数");
}
}
}
实操练习: 修改number的值,观察不同输出
案例2:多条件判断 - 成绩等级评定
public class GradeEvaluator {
public static void main(String[] args) {
int score = 85;
if (score >= 90 && score <= 100) {
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 if (score >= 0) {
System.out.println("不及格");
} else {
System.out.println("无效分数");
}
}
}
实操要点: 注意条件顺序,范围从高到低
案例3:switch语句 - 星期判断
import java.util.Scanner;
public class WeekdayTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入星期数(1-7): ");
int day = scanner.nextInt();
switch (day) {
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:
System.out.println("无效输入,请输入1-7");
}
scanner.close();
}
}
案例4:综合实战 - 登录验证系统
import java.util.Scanner;
public class LoginSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 预设的用户名和密码
String validUsername = "admin";
String validPassword = "123456";
int maxAttempts = 3;
System.out.println("=== 登录系统 ===");
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
System.out.print("用户名: ");
String username = scanner.nextLine();
System.out.print("密码: ");
String password = scanner.nextLine();
// 条件判断
if (username.equals(validUsername) && password.equals(validPassword)) {
System.out.println("登录成功!欢迎回来," + username);
break;
} else {
if (attempt < maxAttempts) {
System.out.println("登录失败!剩余尝试次数: " + (maxAttempts - attempt));
} else {
System.out.println("登录失败!账号已被锁定,请稍后再试");
}
}
}
scanner.close();
}
}
案例5:嵌套条件 - 计算器
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入第一个数字: ");
double num1 = scanner.nextDouble();
System.out.print("请输入运算符(+,-,*,/): ");
char operator = scanner.next().charAt(0);
System.out.print("请输入第二个数字: ");
double num2 = scanner.nextDouble();
double result;
if (operator == '+') {
result = num1 + num2;
System.out.println("结果: " + result);
} else if (operator == '-') {
result = num1 - num2;
System.out.println("结果: " + result);
} else if (operator == '*') {
result = num1 * num2;
System.out.println("结果: " + result);
} else if (operator == '/') {
// 嵌套判断,防止除以0
if (num2 != 0) {
result = num1 / num2;
System.out.println("结果: " + result);
} else {
System.out.println("错误:除数不能为0");
}
} else {
System.out.println("无效运算符");
}
scanner.close();
}
}
实操建议
- 从简单的开始:先写if-else,再尝试switch
- 手动测试:修改输入值,验证每种情况
- 使用IDE调试:
- 设置断点
- 观察变量值变化
- 查看执行流程
- 常见错误排查:
// 错误1:用=代替==
if (score = 100) {} // 错误
// 错误2:字符串用==比较
String s1 = "hello";
String s2 = "hello";
if (s1 == s2) {} // 不推荐,应该用equals()
// 正确写法
if (s1.equals(s2)) {}
进阶练习
试试自己实现这些功能:
- 闰年判断:输入年份,判断是否是闰年
- 三角形类别:输入三边,判断三角形类型(等边、等腰、直角)
- 简易选课系统:根据年级和兴趣推荐课程
需要我详细讲解某个案例的实现思路吗?