GuessNumberGame
This commit is contained in:
parent
267f444f16
commit
5528b24d76
|
@ -16,32 +16,28 @@ public class Main {
|
||||||
System.out.println("3. 困难模式(最多5次猜测)");
|
System.out.println("3. 困难模式(最多5次猜测)");
|
||||||
|
|
||||||
// 获取用户选择的难度
|
// 获取用户选择的难度
|
||||||
int difficulty = 0;
|
int difficulty = getDifficulty(scanner);
|
||||||
while (difficulty < 1 || difficulty > 3) {
|
|
||||||
System.out.print("请输入难度(1/2/3): ");
|
|
||||||
difficulty = scanner.nextInt();
|
|
||||||
if (difficulty < 1 || difficulty > 3) {
|
|
||||||
System.out.println("无效输入,请重新选择!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 根据难度设置最大猜测次数
|
// 根据难度设置最大猜测次数和随机数范围
|
||||||
int maxTries = 100; // 默认为简单模式(最多100次)
|
int maxTries = 100; // 默认为简单模式(最多100次)
|
||||||
|
int upperBound = 100; // 默认随机数范围(1到100)
|
||||||
if (difficulty == 2) {
|
if (difficulty == 2) {
|
||||||
maxTries = 10;
|
maxTries = 10;
|
||||||
} else if (difficulty == 3) {
|
} else if (difficulty == 3) {
|
||||||
maxTries = 5;
|
maxTries = 5;
|
||||||
|
upperBound = 200; // 困难模式下随机数范围更大
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成随机数
|
// 生成随机数
|
||||||
int numberToGuess = random.nextInt(100) + 1; // 生成1到100之间的随机数
|
int numberToGuess = random.nextInt(upperBound) + 1;
|
||||||
|
|
||||||
System.out.println("我已经想好了一个1到100之间的数字。");
|
System.out.println("我已经想好了一个1到" + upperBound + "之间的数字。");
|
||||||
|
|
||||||
// 开始游戏循环
|
// 开始游戏循环
|
||||||
|
boolean hasGuessedCorrectly = false;
|
||||||
for (int numberOfTries = 0; numberOfTries < maxTries; numberOfTries++) {
|
for (int numberOfTries = 0; numberOfTries < maxTries; numberOfTries++) {
|
||||||
System.out.print("请输入你的猜测: ");
|
System.out.print("请输入你的猜测(剩余次数: " + (maxTries - numberOfTries) + "): ");
|
||||||
int guess = scanner.nextInt();
|
int guess = getValidGuess(scanner);
|
||||||
|
|
||||||
if (guess < numberToGuess) {
|
if (guess < numberToGuess) {
|
||||||
System.out.println("太小了,再试一次!");
|
System.out.println("太小了,再试一次!");
|
||||||
|
@ -49,19 +45,18 @@ public class Main {
|
||||||
System.out.println("太大了,再试一次!");
|
System.out.println("太大了,再试一次!");
|
||||||
} else {
|
} else {
|
||||||
System.out.println("恭喜你,猜对了!你总共猜了 " + (numberOfTries + 1) + " 次。");
|
System.out.println("恭喜你,猜对了!你总共猜了 " + (numberOfTries + 1) + " 次。");
|
||||||
|
hasGuessedCorrectly = true;
|
||||||
break; // 猜中后退出当前游戏循环
|
break; // 猜中后退出当前游戏循环
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// 如果达到最大猜测次数且未猜中
|
|
||||||
if (numberOfTries == maxTries - 1) {
|
// 如果达到最大猜测次数且未猜中
|
||||||
System.out.println("很遗憾,你已经用完了所有猜测机会。正确答案是:" + numberToGuess);
|
if (!hasGuessedCorrectly) {
|
||||||
}
|
System.out.println("很遗憾,你已经用完了所有猜测机会。正确答案是:" + numberToGuess);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 询问用户是否继续游戏
|
// 询问用户是否继续游戏
|
||||||
System.out.print("是否继续游戏?(y/n): ");
|
if (!askToContinue(scanner)) {
|
||||||
String continueChoice = scanner.next().toLowerCase();
|
|
||||||
if (!continueChoice.equals("y")) {
|
|
||||||
System.out.println("感谢游玩,再见!");
|
System.out.println("感谢游玩,再见!");
|
||||||
break; // 用户选择不继续时退出外层循环
|
break; // 用户选择不继续时退出外层循环
|
||||||
}
|
}
|
||||||
|
@ -69,4 +64,47 @@ public class Main {
|
||||||
|
|
||||||
scanner.close();
|
scanner.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户选择的难度(1/2/3)
|
||||||
|
*/
|
||||||
|
private static int getDifficulty(Scanner scanner) {
|
||||||
|
int difficulty = 0;
|
||||||
|
while (difficulty < 1 || difficulty > 3) {
|
||||||
|
System.out.print("请输入难度(1/2/3): ");
|
||||||
|
if (scanner.hasNextInt()) {
|
||||||
|
difficulty = scanner.nextInt();
|
||||||
|
if (difficulty < 1 || difficulty > 3) {
|
||||||
|
System.out.println("无效输入,请重新选择!");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
System.out.println("请输入有效的数字!");
|
||||||
|
scanner.next(); // 清除无效输入
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return difficulty;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户的有效猜测(必须是数字)
|
||||||
|
*/
|
||||||
|
private static int getValidGuess(Scanner scanner) {
|
||||||
|
while (true) {
|
||||||
|
if (scanner.hasNextInt()) {
|
||||||
|
return scanner.nextInt();
|
||||||
|
} else {
|
||||||
|
System.out.println("请输入有效的数字!");
|
||||||
|
scanner.next(); // 清除无效输入
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 询问用户是否继续游戏
|
||||||
|
*/
|
||||||
|
private static boolean askToContinue(Scanner scanner) {
|
||||||
|
System.out.print("是否继续游戏?(y/n): ");
|
||||||
|
String continueChoice = scanner.next().toLowerCase();
|
||||||
|
return continueChoice.equals("y");
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user