循环结构
循环让程序可以重复执行某些操作,本课学习Java中的循环结构。
for循环
for循环是最常用的循环结构,适合已知循环次数的场景。
语法
JAVA
for (初始化; 条件; 更新) {
// 循环体
}
示例:打印1到10
JAVA
public class ForDemo {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}
执行流程
TEXT
1. 执行初始化(int i = 1)
2. 判断条件(i <= 10)
3. 如果条件为true,执行循环体
4. 执行更新(i++)
5. 回到步骤2
示例:求1到100的和
JAVA
public class SumDemo {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
System.out.println("1到100的和:" + sum); // 5050
}
}
while循环
while循环适合不确定循环次数的场景。
语法
JAVA
while (条件) {
// 循环体
}
示例:猜数字游戏
JAVA
import java.util.Scanner;
import java.util.Random;
public class GuessGame {
public static void main(String[] args) {
Random random = new Random();
int target = random.nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
int guess = 0;
System.out.println("猜数字游戏(1-100)");
while (guess != target) {
System.out.print("请输入你的猜测:");
guess = scanner.nextInt();
if (guess > target) {
System.out.println("太大了!");
} else if (guess < target) {
System.out.println("太小了!");
} else {
System.out.println("恭喜你猜对了!");
}
}
scanner.close();
}
}
do-while循环
do-while循环至少执行一次循环体。
语法
JAVA
do {
// 循环体
} while (条件);
示例:菜单循环
JAVA
import java.util.Scanner;
public class MenuDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("\n=== 主菜单 ===");
System.out.println("1. 开始游戏");
System.out.println("2. 设置");
System.out.println("3. 退出");
System.out.print("请选择:");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("游戏开始...");
break;
case 2:
System.out.println("打开设置...");
break;
case 3:
System.out.println("再见!");
break;
default:
System.out.println("无效选择");
}
} while (choice != 3);
scanner.close();
}
}
💡 while vs do-while: while先判断后执行,可能一次都不执行。do-while先执行后判断,至少执行一次。
for-each循环
for-each循环用于遍历数组或集合。
语法
JAVA
for (类型 变量 : 数组或集合) {
// 使用变量
}
示例:遍历数组
JAVA
public class ForEachDemo {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
// 普通for循环
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
// for-each循环
for (int num : numbers) {
System.out.println(num);
}
}
}
💡 选择建议: 只需要遍历元素时用for-each,需要索引时用普通for。
break和continue
break:跳出循环
JAVA
public class BreakDemo {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // 当i等于5时,跳出整个循环
}
System.out.println(i);
}
// 输出:1 2 3 4
}
}
continue:跳过本次循环
JAVA
public class ContinueDemo {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // 跳过偶数
}
System.out.println(i);
}
// 输出:1 3 5 7 9
}
}
嵌套循环
循环可以嵌套使用。
示例:九九乘法表
JAVA
public class MultiplicationTable {
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) + "\t");
}
System.out.println();
}
}
}
输出:
TEXT
1 × 1 = 1
1 × 2 = 2 2 × 2 = 4
1 × 3 = 3 2 × 3 = 6 3 × 3 = 9
...
示例:打印三角形
JAVA
public class TriangleDemo {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
// 打印空格
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
// 打印星号
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
输出:
TEXT
*
***
*
***
*****
死循环
如果循环条件永远为true,就会形成死循环。
JAVA
// 死循环示例(不要这样写)
while (true) {
System.out.println("无限循环");
}
// 正确的无限循环(需要有退出条件)
while (true) {
// 读取用户输入
String input = scanner.nextLine();
if (input.equals("exit")) {
break; // 输入exit时退出
}
}
❓ 常见问题
Q for和while怎么选?
A 已知循环次数用for,不确定次数用while。for更简洁,while更灵活。
Q break和return有什么区别?
A break跳出当前循环,return结束整个方法。
Q 如何跳出多层循环?
A 使用带标签的break。
outer: for(...) { for(...) { break outer; } }📖 小节
- for循环适合已知次数,while适合不确定次数
- do-while至少执行一次循环体
- for-each用于遍历数组或集合
- break跳出循环,continue跳过本次循环
- 循环可以嵌套,注意避免死循环
📝 作业
- 求和: 计算1到100中所有奇数的和
- 阶乘: 计算10的阶乘(10! = 10 × 9 × ... × 1)
- 九九乘法表: 使用嵌套循环打印完整的九九乘法表
下一课
下一课我们将学习 数组,了解如何存储和操作一组数据。



