本文目录导读:

我来为您详细介绍Java中if判断的各种案例和编写方法。
基础if判断
单重if判断
public class IfBasic {
public static void main(String[] args) {
int score = 85;
// 基本if语句
if (score >= 60) {
System.out.println("及格了!");
}
}
}
if-else判断
public class IfElseExample {
public static void main(String[] args) {
int age = 18;
if (age >= 18) {
System.out.println("成年人");
} else {
System.out.println("未成年人");
}
}
}
多重if-else判断
public class MultipleIfElse {
public static void main(String[] args) {
int score = 85;
char 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判断
public class NestedIfExample {
public static void main(String[] args) {
int age = 25;
boolean hasLicense = true;
// 嵌套if判断
if (age >= 18) {
System.out.println("年龄符合要求");
if (hasLicense) {
System.out.println("可以驾驶汽车");
} else {
System.out.println("请先考取驾照");
}
} else {
System.out.println("年龄不符合驾驶要求");
}
}
}
逻辑运算符的使用
public class LogicalOperatorsExample {
public static void main(String[] args) {
int age = 25;
double height = 175.5;
boolean isHealthy = true;
// 使用逻辑与(&&)
if (age >= 18 && age <= 60) {
System.out.println("年龄在18-60岁之间");
}
// 使用逻辑或(||)
if (height < 150 || height > 200) {
System.out.println("身高超出正常范围");
}
// 使用逻辑非(!)
if (!isHealthy) {
System.out.println("需要休息");
} else {
System.out.println("身体健康");
}
// 组合使用
if ((age >= 18 && age <= 60) && (height >= 150 && height <= 200) && isHealthy) {
System.out.println("符合参军条件");
}
}
}
实际应用案例
案例1:登录验证
import java.util.Scanner;
public class LoginSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String correctUsername = "admin";
String correctPassword = "123456";
System.out.print("请输入用户名:");
String username = scanner.nextLine();
System.out.print("请输入密码:");
String password = scanner.nextLine();
// 验证登录
if (username.equals(correctUsername) && password.equals(correctPassword)) {
System.out.println("登录成功!");
} else if (!username.equals(correctUsername)) {
System.out.println("用户名错误!");
} else {
System.out.println("密码错误!");
}
scanner.close();
}
}
案例2:判断闰年
import java.util.Scanner;
public class LeapYearCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年份:");
int year = scanner.nextInt();
// 闰年判断条件:能被4整除但不能被100整除,或者能被400整除
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
System.out.println(year + "年是闰年");
} else {
System.out.println(year + "年不是闰年");
}
scanner.close();
}
}
案例3:计算器功能(带验证)
import java.util.Scanner;
public class CalculatorWithValidation {
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(num1 + " + " + num2 + " = " + result);
} else if (operator == '-') {
result = num1 - num2;
System.out.println(num1 + " - " + num2 + " = " + result);
} else if (operator == '*') {
result = num1 * num2;
System.out.println(num1 + " * " + num2 + " = " + result);
} else if (operator == '/') {
if (num2 == 0) {
System.out.println("错误:除数不能为0!");
} else {
result = num1 / num2;
System.out.println(num1 + " / " + num2 + " = " + result);
}
} else {
System.out.println("错误:无效的运算符!");
}
scanner.close();
}
}
三元运算符(if的简写形式)
public class TernaryExample {
public static void main(String[] args) {
int score = 75;
// 使用if-else
String result1;
if (score >= 60) {
result1 = "及格";
} else {
result1 = "不及格";
}
// 使用三元运算符(等价写法)
String result2 = (score >= 60) ? "及格" : "不及格";
System.out.println("result1 = " + result1);
System.out.println("result2 = " + result2);
}
}
注意事项和最佳实践
public class BestPractices {
public static void main(String[] args) {
// 1. 避免浮点数直接比较
double a = 0.1 + 0.2;
double b = 0.3;
// 错误的写法
// if (a == b)
// 正确的写法
double epsilon = 0.0001;
if (Math.abs(a - b) < epsilon) {
System.out.println("a 和 b 相等");
}
// 2. 字符串比较使用equals()
String str1 = "hello";
String str2 = "hello";
// 正确的写法
if (str1.equals(str2)) {
System.out.println("字符串相等");
}
// 3. 避免复杂的嵌套条件
int age = 20;
int income = 50000;
boolean hasCar = false;
// 不好的写法:过多嵌套
if (age > 18) {
if (income > 30000) {
if (!hasCar) {
System.out.println("可以考虑贷款买车");
}
}
}
// 好的写法:使用逻辑运算符
if (age > 18 && income > 30000 && !hasCar) {
System.out.println("可以考虑贷款买车");
}
}
}
编写if判断时的关键点:
- 使用 比较基本类型,使用
equals()比较对象 - 避免过深的嵌套,可以用逻辑运算符优化
- 注意边界条件的处理
- 使用三元运算符简化简单的if-else
- 对于浮点数比较,使用范围判断而非精确相等
希望这些案例能帮助您更好地理解和使用Java中的if判断!