掌握使用 Math.random() 或 Random 类生成随机数
熟练运用 Scanner 处理用户输入
灵活使用 for/while 循环控制游戏回合
运用 if-else 条件判断提供实时反馈
使用 return 实现方法提前退出
设计可扩展的游戏逻辑(如无限轮次、计分系统)
理解基础游戏开发中的控制流设计
这是一个经典的人机互动猜数游戏:
计算机在 1 到 100 范围内随机选择一个整数
玩家有 K 次机会(例如 5 次)猜测该数字
每次猜测后,程序会提示:
“太大了,请猜小一点”
“太小了,请猜大一点”
若玩家在 K 次内猜中 → 胜利!
若 K 次用完仍未猜中 → 游戏结束,公布答案
价值:综合训练循环、条件、输入输出、随机数等核心 Java 基础技能
Math.random() 生成 [0.0, 1.0) 的浮点数
(int)(100 * Math.random()) + 1 → 转换为 1~100 的整数
for 循环限制尝试次数
return 在猜中时立即退出方法
import java.util.Scanner;
public class NumberGuessingGame {
public static void playGuessingGame() {
Scanner scanner = new Scanner(System.in);
// 生成 1~100 的随机整数
int targetNumber = 1 + (int) (100 * Math.random());
int maxAttempts = 5; // 最大尝试次数
System.out.println("我已经想好了一个 1 到 100 之间的数字。");
System.out.println("你有 " + maxAttempts + " 次机会来猜中它!");
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
System.out.print("请输入你的第 " + attempt + " 次猜测:");
int userGuess = scanner.nextInt();
if (userGuess == targetNumber) {
System.out.println("恭喜你!猜对了!答案就是 " + targetNumber + "。");
scanner.close();
return; // 提前退出方法
} else if (userGuess < targetNumber) {
System.out.println("太小了!正确答案比 " + userGuess + " 大。");
} else {
System.out.println("太大了!正确答案比 " + userGuess + " 小。");
}
// 提示剩余次数(非最后一次)
if (attempt < maxAttempts) {
System.out.println("你还剩下 " + (maxAttempts - attempt) + " 次机会。\n");
}
}
// 尝试次数用尽
System.out.println("很遗憾,你已经用完了所有 " + maxAttempts + " 次机会。");
System.out.println("正确答案是:" + targetNumber);
scanner.close();
}
public static void main(String[] args) {
playGuessingGame();
}
}我已经想好了一个 1 到 100 之间的数字。
你有 5 次机会来猜中它!
请输入你的第 1 次猜测:50
太大了!正确答案比 50 小。
你还剩下 4 次机会。
请输入你的第 2 次猜测:25
太小了!正确答案比 25 大。
你还剩下 3 次机会。
请输入你的第 3 次猜测:37
太大了!正确答案比 37 小。
你还剩下 2 次机会。
请输入你的第 4 次猜测:31
太小了!正确答案比 31 大。
你还剩下 1 次机会。
请输入你的第 5 次猜测:34
恭喜你!猜对了!答案就是 34。替代方案:也可使用
java.util.RandomRandom random = new Random(); int targetNumber = random.nextInt(100) + 1;
允许玩家在失败后继续游戏(不重新生成数字)
记录总尝试次数(用于评分)
使用 while + for 嵌套实现“每轮 K 次”机制
用户输入 "yes" 可继续,否则退出
import java.util.Scanner;
public class AdvancedNumberGuessingGame {
public static void playGuessingGame() {
Scanner scanner = new Scanner(System.in);
int targetNumber = 1 + (int) (100 * Math.random());
int totalAttempts = 0;
int attemptsPerRound = 5;
boolean hasGuessedCorrectly = false;
System.out.println("我已经想好了一个 1 到 100 之间的数字。");
System.out.println("每轮你有 " + attemptsPerRound + " 次猜测机会。");
while (!hasGuessedCorrectly) {
// 每轮最多 K 次尝试
for (int i = 0; i < attemptsPerRound; i++) {
System.out.print("请输入你的猜测:");
int guess = scanner.nextInt();
totalAttempts++;
if (guess == targetNumber) {
System.out.println("恭喜你!你用了 " + totalAttempts + " 次,猜中了数字 " + targetNumber + "!");
hasGuessedCorrectly = true;
break; // 跳出本轮 for 循环
} else if (guess < targetNumber) {
System.out.println("太小了!正确答案比 " + guess + " 大。");
} else {
System.out.println("太大了!正确答案比 " + guess + " 小。");
}
}
// 如果本轮未猜中,询问是否继续
if (!hasGuessedCorrectly) {
System.out.println("本轮 " + attemptsPerRound + " 次机会已用完。");
System.out.print("是否继续猜测?(输入 yes 继续,其他任意键退出):");
String response = scanner.next();
if (!response.equalsIgnoreCase("yes")) {
System.out.println("游戏结束!正确答案是:" + targetNumber);
break; // 退出 while 循环
}
System.out.println("好的,继续猜测!当前数字不变。\n");
}
}
scanner.close();
}
public static void main(String[] args) {
playGuessingGame();
}
}我已经想好了一个 1 到 100 之间的数字。
每轮你有 5 次猜测机会。
请输入你的猜测:50
太大了!正确答案比 50 小。
...(5 次用完)...
本轮 5 次机会已用完。
是否继续猜测?(输入 yes 继续,其他任意键退出):yes
好的,继续猜测!当前数字不变。
请输入你的猜测:25
太小了!正确答案比 25 大。
请输入你的猜测:37
恭喜你!你用了 7 次,猜中了数字 34!当前程序若输入非整数(如字母),会抛出 InputMismatchException。可添加异常处理:
while (!scanner.hasNextInt()) {
System.out.println("输入无效!请输入一个整数:");
scanner.next(); // 清除无效输入
}
int guess = scanner.nextInt();允许玩家选择范围(150 / 1000 / 1 ~ 1000)
根据范围动态调整尝试次数
猜中或放弃后,询问“是否再玩一局?”
每局生成新随机数
记录最少尝试次数
显示历史最佳表现
异常处理:修改基础版代码,使其在用户输入非整数时不崩溃,而是提示重新输入。
游戏重玩:在进阶版基础上,增加“是否开始新一局”的选项,每局生成新数字。
性能分析:理论上,猜 1~100 的数字,最少需要几次?最多需要几次?如何用二分查找策略优化猜测?
面向对象改造:将游戏逻辑封装为 GuessingGame 类,包含属性(number, attempts, maxAttempts)和方法(start(), makeGuess())。