本文目录导读:

我来为你介绍几个Java while循环的经典案例。
基础while循环案例
案例1:数字累加(1到100)
public class WhileDemo1 {
public static void main(String[] args) {
int i = 1;
int sum = 0;
while (i <= 100) {
sum += i;
i++;
}
System.out.println("1到100的和为:" + sum);
}
}
案例2:猜数字游戏
import java.util.Scanner;
public class GuessNumber {
public static void main(String[] args) {
int answer = (int)(Math.random() * 100) + 1; // 1-100随机数
Scanner scanner = new Scanner(System.in);
int guess;
int attempts = 0;
System.out.println("猜数字游戏开始!(1-100)");
while (true) {
System.out.print("请输入你猜的数字:");
guess = scanner.nextInt();
attempts++;
if (guess > answer) {
System.out.println("太大了!");
} else if (guess < answer) {
System.out.println("太小了!");
} else {
System.out.println("恭喜你猜对了!用了" + attempts + "次");
break;
}
}
scanner.close();
}
}
实用场景案例
案例3:密码验证
import java.util.Scanner;
public class PasswordCheck {
public static void main(String[] args) {
final String PASSWORD = "123456";
Scanner scanner = new Scanner(System.in);
int attempts = 0;
final int MAX_ATTEMPTS = 3;
System.out.println("请输入密码(最多3次机会):");
while (attempts < MAX_ATTEMPTS) {
System.out.print("密码:");
String input = scanner.nextLine();
if (input.equals(PASSWORD)) {
System.out.println("登录成功!");
break;
} else {
attempts++;
if (attempts < MAX_ATTEMPTS) {
int remaining = MAX_ATTEMPTS - attempts;
System.out.println("密码错误!还剩" + remaining + "次机会");
} else {
System.out.println("账户已锁定!");
}
}
}
scanner.close();
}
}
案例4:计算阶乘
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个正整数:");
int n = scanner.nextInt();
int i = 1;
long result = 1;
while (i <= n) {
result *= i;
i++;
}
System.out.println(n + "! = " + result);
scanner.close();
}
}
do-while循环案例
案例5:菜单程序
import java.util.Scanner;
public class MenuSystem {
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.println("4. 退出系统");
System.out.print("请选择功能(1-4):");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("您的余额为:1000.00元");
break;
case 2:
System.out.println("存款功能执行中...");
break;
case 3:
System.out.println("取款功能执行中...");
break;
case 4:
System.out.println("感谢使用,再见!");
break;
default:
System.out.println("无效选择,请重新输入!");
}
} while (choice != 4);
scanner.close();
}
}
数据验证案例
案例6:用户输入验证
import java.util.Scanner;
public class InputValidation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int age;
System.out.println("请输入您的年龄:");
// 循环直到输入有效数据
while (true) {
if (scanner.hasNextInt()) {
age = scanner.nextInt();
if (age > 0 && age < 150) {
System.out.println("年龄有效:" + age);
break;
} else {
System.out.println("年龄范围必须在1-149之间,请重新输入:");
}
} else {
System.out.println("请输入数字,请重新输入:");
scanner.next(); // 清除错误输入
}
}
scanner.close();
}
}
while循环嵌套案例
案例7:乘法表
public class MultiplicationTable {
public static void main(String[] args) {
int i = 1;
while (i <= 9) {
int j = 1;
while (j <= i) {
System.out.print(j + "×" + i + "=" + (i * j) + "\t");
j++;
}
System.out.println(); // 换行
i++;
}
}
}
编写while循环的建议
- 注意循环条件:确保循环能够结束,避免无限循环
- 计数器更新:不要忘记更新循环变量
- break使用:适当使用break提前退出循环
- 性能考虑:避免在循环体内做过多计算
- 代码可读性:保持循环体简洁,复杂逻辑可以提取为方法
这些案例涵盖了while循环的主要应用场景,你可以根据实际需求选择合适的模式。