用java写一个小型游戏,游戏类型任意
时间: 2023-12-19 10:05:08 浏览: 29
这是一个简单的猜数字小游戏,玩家需要猜出随机生成的数字。
```java
import java.util.Scanner;
public class GuessNumber {
public static void main(String[] args) {
System.out.println("欢迎来到猜数字游戏!");
System.out.println("请猜出1-100之间的一个数字。");
// 生成随机数
int randomNumber = (int) (Math.random() * 100) + 1;
// 游戏循环
boolean isWin = false;
Scanner scanner = new Scanner(System.in);
for (int i = 1; i <= 10; i++) {
System.out.println("第" + i + "次猜测,请输入数字:");
int guessNumber = scanner.nextInt();
if (guessNumber == randomNumber) {
isWin = true;
break;
} else if (guessNumber < randomNumber) {
System.out.println("您猜的数字太小了,请继续猜测。");
} else {
System.out.println("您猜的数字太大了,请继续猜测。");
}
}
// 游戏结果
if (isWin) {
System.out.println("恭喜您,猜对了!");
} else {
System.out.println("很遗憾,您没有在规定次数内猜对。正确答案是:" + randomNumber);
}
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)