java完整代码int count = 0; while (count < 5) { int x = (int) (Math.random() * 6); int y = (int) (Math.random() * 6); if (map[x][y] == 0) { map[x][y] = 1; count++; } }
时间: 2024-01-29 18:02:46 浏览: 36
好的,这是完整的Java代码,包括生成地图、放老鼠、打印地图、用户输入、判断打中老鼠、玩家血量等功能:
```
import java.util.Scanner;
public class WhacAMole {
public static void main(String[] args) {
// 生成地图
int[][] map = new int[6][6];
// 放老鼠
int count = 0;
while (count < 5) {
int x = (int) (Math.random() * 6);
int y = (int) (Math.random() * 6);
if (map[x][y] == 0) {
map[x][y] = 1;
count++;
}
}
// 打印地图
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
// 用户输入
Scanner scanner = new Scanner(System.in);
System.out.print("请输入横坐标:");
int x = scanner.nextInt();
System.out.print("请输入纵坐标:");
int y = scanner.nextInt();
// 判断打中老鼠
if (map[x][y] == 1) {
System.out.println("打中老鼠了!");
// 玩家加血量1
} else {
System.out.println("没打中老鼠!");
// 玩家扣血量1
}
// 玩家血量
int health = 2;
if (map[x][y] == 1) {
health += 1;
} else {
health -= 1;
}
System.out.println("当前血量为:" + health);
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)