This commit is contained in:
KuaiLeTianShi 2025-02-13 07:18:08 +08:00
parent a53b66ac48
commit 1abfd4ed95
7 changed files with 140 additions and 0 deletions

29
GuessNumberGame/.gitignore vendored Normal file
View File

@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
GuessNumberGame/.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="corretto-11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/GuessNumberGame.iml" filepath="$PROJECT_DIR$/GuessNumberGame.iml" />
</modules>
</component>
</project>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,72 @@
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Random random = new Random();
Scanner scanner = new Scanner(System.in);
// 外层循环允许用户多次玩游戏
while (true) {
// 游戏开始提示
System.out.println("欢迎来到猜数字游戏!");
System.out.println("请选择游戏难度:");
System.out.println("1. 简单模式最多100次猜测");
System.out.println("2. 中等模式最多10次猜测");
System.out.println("3. 困难模式最多5次猜测");
// 获取用户选择的难度
int difficulty = 0;
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次
if (difficulty == 2) {
maxTries = 10;
} else if (difficulty == 3) {
maxTries = 5;
}
// 生成随机数
int numberToGuess = random.nextInt(100) + 1; // 生成1到100之间的随机数
System.out.println("我已经想好了一个1到100之间的数字。");
// 开始游戏循环
for (int numberOfTries = 0; numberOfTries < maxTries; numberOfTries++) {
System.out.print("请输入你的猜测: ");
int guess = scanner.nextInt();
if (guess < numberToGuess) {
System.out.println("太小了,再试一次!");
} else if (guess > numberToGuess) {
System.out.println("太大了,再试一次!");
} else {
System.out.println("恭喜你,猜对了!你总共猜了 " + (numberOfTries + 1) + " 次。");
break; // 猜中后退出当前游戏循环
}
// 如果达到最大猜测次数且未猜中
if (numberOfTries == maxTries - 1) {
System.out.println("很遗憾,你已经用完了所有猜测机会。正确答案是:" + numberToGuess);
}
}
// 询问用户是否继续游戏
System.out.print("是否继续游戏?(y/n): ");
String continueChoice = scanner.next().toLowerCase();
if (!continueChoice.equals("y")) {
System.out.println("感谢游玩,再见!");
break; // 用户选择不继续时退出外层循环
}
}
scanner.close();
}
}