一、Java 条件语句(if-else)
在 Java 中,条件语句用于控制程序的执行流程,根据特定的条件决定执行哪一部分代码。
1. if-else 语句
基本语法
if (条件) {
// 条件为 true 时执行的代码
} else {
// 条件为 false 时执行的代码
}
示例:判断一个数是奇数还是偶数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
if (num % 2 == 0) {
System.out.println("偶数");
} else {
System.out.println("奇数");
}
}
}
2. if-else if-else 语句
用于多个条件的判断
if (条件1) {
// 条件1为真时执行
} else if (条件2) {
// 条件2为真时执行
} else {
// 所有条件都不满足时执行
}
示例:根据分数判断等级
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
if (score >= 90) {
System.out.println("优秀");
} else if (score >= 80) {
System.out.println("良好");
} else if (score >= 60) {
System.out.println("及格");
} else {
System.out.println("不及格");
}
}
}
3. 三元运算符
简化 if-else 语句的写法
变量 = (条件) ? 表达式1 : 表达式2;
示例:判断最小值
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int min = (a < b) ? a : b;
System.out.println("最小值:" + min);
}
}
二、循环语句(for / while)
循环语句用于重复执行代码,直到满足终止条件。
1. for 循环
语法:
for (初始化; 条件; 更新) {
// 循环体
}
示例:输出 1-10
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.print(i + " ");
}
}
}
2. while 循环
语法:
while (条件) {
// 循环体
}
示例:计算 1-100 的和
public class Main {
public static void main(String[] args) {
int sum = 0, i = 1;
while (i <= 100) {
sum += i;
i++;
}
System.out.println("总和:" + sum);
}
}
3. do-while 循环
语法:
do {
// 循环体
} while (条件);
示例:至少执行一次的循环
public class Main {
public static void main(String[] args) {
int num = 10;
do {
System.out.println(num);
num--;
} while (num > 5);
}
}
do-while 的特点:
- 至少执行一次,即使条件一开始就是 false,仍然会执行循环体一次。
4. for-each 循环(增强型 for)
用于遍历数组或集合
for (数据类型 变量 : 数组或集合) {
// 处理变量
}
示例:遍历数组
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
System.out.println(num);
}
}
}
三、练习题
1. 判断闰年
题目描述: 输入一个年份,判断它是否是闰年。
闰年规则:
- 能被 4 整除但不能被 100 整除,或者能被 400 整除。
输入示例:
2000
输出示例:
闰年
Java 代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.println("闰年");
} else {
System.out.println("平年");
}
}
}
2. 计算 n!(阶乘)
题目描述: 输入一个整数 n,计算 n!(n 的阶乘)。
输入示例:
5
输出示例:
120
Java 代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
System.out.println(fact);
}
}
3. 输出九九乘法表
题目描述: 输出标准的九九乘法表。
输出示例:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
...
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + (i * j) + " ");
}
System.out.println();
}
}
}
- 题目:判断素数
题目描述
给定一个正整数,判断它是否为素数(质数)。素数是指一个大于 1 的自然数,除了 1 和它自身外,不能被其他自然数整除的数。
import java.util.Scanner;
public class PrimeNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
boolean isPrime = true;
if (n < 2) {
isPrime = false;
} else {
for (int i = 2; i < n; i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
System.out.println("Yes");
} else {
System.out.println("No");
}
scanner.close();
}
}
5.题目2:斐波那契数列
描述:输出前n项斐波那契数列。
import java.util.Scanner;
public class FibonacciIterative {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
if (n == 1) {
System.out.println(0);
} else if (n == 2) {
System.out.println("0 1");
} else {
int[] fib = new int[n];
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i < n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
for (int i = 0; i < n - 1; i++) {
System.out.print(fib[i] + " ");
}
System.out.println(fib[n - 1]);
}
scanner.close();
}
}
四.知识点总结
- 条件表达式的结果必须是布尔类型(true 或 false)。
- if - else 语句可以嵌套使用,以实现更复杂的条件判断。
- 在使用多分支 if - else if - else 时,条件判断是按顺序进行的,一旦某个条件为 true,后面的条件将不再判断。
- for 循环适合已知循环次数的场景,循环变量的初始化、条件判断和迭代都在一个语句中,结构紧凑。
- while 循环和 do - while 循环适合未知循环次数,只知道终止条件的场景。do - while 循环保证循环体至少执行一次。
- 在循环中,可以使用 break 语句提前终止整个循环,使用 continue 语句跳过本次循环的剩余
五.易错点:
- 忽略世纪年(如1900年不是闰年,2000年是闰年)。
- 条件顺序错误(如先判断year % 400 == 0,否则可能被year % 100拦截)。部分,直接进入下一次循环。
- 未处理n=0的情况(0的阶乘是1)。
- 阶乘结果可能超出int范围(如12! = 479001600,但13!会溢出)。
六.常见错误与避坑指南
1. 条件语句常见错误
比较浮点数:直接使用==比较浮点数(如if (a == 0.1)),应改用误差范围判断。
逻辑运算符混淆:误用&(按位与)代替&&(逻辑与)。
空代码块:无意义的if语句(如if (condition);,后面的分号导致逻辑错误)。
2. 循环语句常见错误
死循环:未正确更新循环变量(如while (i < 10) { ... }中忘记i++)。
越界访问:循环条件错误导致数组越界(如for (int i=0; i<=arr.length; i++))。
初始化遗漏:未初始化循环控制变量(如int i; while (i < 10) { ... })。
3. 代码风格问题
缩进混乱:未对齐if-else或循环代码块,降低可读性。
冗余条件:复杂的条件表达式可简化(如if (a > 0 && a != 5)可合并逻辑)。
如果这篇文章对你有帮助,点个关注吧