数字炸弹小游戏\n都玩过的,想一个数字,比如0-100以内,别人去猜,然后说大了还是小了。 详细代码如下:
import java.util.HashMap; import java.util.Map; import java.util.Random; import java.util.Scanner;
public class NumberBombGame { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
// 输入数字范围
System.out.println("请输入数字范围:");
int rangeStart = scanner.nextInt(); // 范围开始
int rangeEnd = scanner.nextInt(); // 范围结束
// 输入玩家名称和数量
System.out.println("请输入玩家数量:");
int playerNum = scanner.nextInt();
scanner.nextLine(); // 需要在nextInt()之后使用nextLine()换行符才能读取正常名称
// 初始化玩家分数
Map<String, Integer> playerScores = new HashMap<>();
for (int i = 1; i <= playerNum; i++) {
System.out.println("请输入玩家" + i + "的名称:");
String playerName = scanner.nextLine();
playerScores.put(playerName, 0);
}
// 随机生成一个数字
Random random = new Random();
int targetNum = random.nextInt(rangeEnd - rangeStart + 1) + rangeStart;
// 循环让每个玩家循环输入猜测的数字
boolean isGameEnded = false;
while (!isGameEnded) {
for (String playerName : playerScores.keySet()) {
System.out.println(playerName + ",请猜测一个数字:");
int guessNum = scanner.nextInt(); // 玩家猜测的数字
if (guessNum == targetNum) { // 如果猜对了,该玩家得分加 1,游戏结束
playerScores.put(playerName, playerScores.get(playerName) + 1);
isGameEnded = true;
break; // 跳出循环
} else { // 猜错了,输出提示信息
String hint = guessNum < targetNum ? "小了" : "大了";
System.out.println("猜错了," + hint + "!");
}
}
}
// 输出每个玩家的分数
System.out.println("游戏结束!");
for (String playerName : playerScores.keySet()) {
System.out.println(playerName + "得分:" + playerScores.get(playerName));
}
}
} 测试结果: 请输入数字范围: 0 20 请输入玩家数量: 2 请输入玩家1的名称: 老王 请输入玩家2的名称: 老李 老李,请猜测一个数字: 10 猜错了,大了! 老王,请猜测一个数字: 5 猜错了,大了! 老李,请猜测一个数字: 1 猜错了,小了! 老王,请猜测一个数字: 3 游戏结束! 老李得分:0 老王得分:1
通过以上代码,我们可以学到以下知识点: 如何使用 Scanner 类从控制台读取输入,包括基本数据类型和字符串类型。 如何使用 Random 类产生随机数。 如何使用 Map 类来保存玩家的得分。 如何使用 while 循环和 break 语句实现循环猜测和游戏结束的判断。 如何使用条件语句来实现猜错了的提示和猜对了的得分计算。 如何使用循环来读取多个玩家的输入。 如何在程序中进行基本的输入输出处理。 2.掷骰子游戏 游戏规则 三个六面骰子,每个骰子点数为:1,2,3,4,5,6; 三个骰子数加起来,1-10为小,11-18为大; 大和小分值1分 如果三个骰子的数值一样,就称为豹子,豹子18分 猜对了就加分,猜错了就扣分 import java.util.Random; import java.util.Scanner;
public class DiceGame { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
int maxRound = 3; // 最大游戏轮数
int totalScore = 0; // 总得分
Random random = new Random();
// 游戏开始
System.out.println("欢迎来到掷骰子游戏!");
// 循环猜测,最多进行三轮游戏
for (int round = 1; round <= maxRound; round++) {
System.out.println("第" + round + "轮游戏开始!");
// 玩家选择猜测的类别
System.out.println("请选择猜测的类别:1.小(1-10) 2.大(11-18) 3.豹子");
int category = scanner.nextInt();
while (category < 1 || category > 3) {
System.out.println("输入错误,请重新选择猜测的类别:1.小(1-10) 2.大(11-18) 3.豹子");
category = scanner.nextInt();
}
// 投掷骰子
int dice1 = random.nextInt(6) + 1;
int dice2 = random.nextInt(6) + 1;
int dice3 = random.nextInt(6) + 1;
int sum = dice1 + dice2 + dice3;
System.out.println("三个骰子的点数分别为:" + dice1 + "," + dice2 + "," + dice3);
// 判断猜测是否正确
int roundScore = 0; // 本轮得分
if (category == 1 && sum >= 1 && sum <= 10) { // 猜小
System.out.println("猜小!");
roundScore = 1;
} else if (category == 2 && sum >= 11 && sum <= 18) { // 猜大
System.out.println("猜大!");
roundScore = 1;
} else if (category == 3 && dice1 == dice2 && dice2 == dice3) { // 豹子
System.out.println("猜中豹子,恭喜获得18分!");
roundScore = 18;
} else {
System.out.println("猜错了!");
if (category == 3) { // 豹子猜错扣18分
roundScore = -18;
} else {
roundScore = -1; // 其他猜错扣1分
}
}
// 累加本轮得分和总得分
totalScore += roundScore;
// 输出本轮得分和总得分
System.out.println("本轮得分为:" + roundScore);
System.out.println("总得分为:" + totalScore);
// 询问是否继续游戏
if (round == maxRound) {
break;
}
System.out.println("是否继续游戏?(1.是 / 2.否)");
int reply;
do {
reply = scanner.nextInt();
} while (reply != 1 && reply != 2);
if (reply == 2) {
break;
}
}
// 输出最终得分
System.out.println("游戏结束!你的最终得分总计为:" + totalScore);
}
} 测试结果如下:
欢迎来到掷骰子游戏! 第1轮游戏开始! 请选择猜测的类别:1.小(1-10) 2.大(11-18) 3.豹子 1 三个骰子的点数分别为:6,4,1 猜错了! 本轮得分为:-1 总得分为:-1 是否继续游戏?(1.是 / 2.否) 1 第2轮游戏开始! 请选择猜测的类别:1.小(1-10) 2.大(11-18) 3.豹子 1 三个骰子的点数分别为:5,5,1 猜错了! 本轮得分为:-1 总得分为:-2 是否继续游戏?(1.是 / 2.否) 1 第3轮游戏开始! 请选择猜测的类别:1.小(1-10) 2.大(11-18) 3.豹子 3 三个骰子的点数分别为:3,6,1 猜错了! 本轮得分为:-18 总得分为:-20 游戏结束!你的最终得分总计为:-20
通过以上代码,可以学到以下知识: 循环结构:使用for循环来实现游戏的多轮进行,以及使用while循环来保证玩家输入正确的猜测类别。 条件分支结构:使用if-else分支结构来根据玩家的猜测类别和投掷骰子的结果来判断是否猜中,从而奖励或扣除相应的分数。\n\n随机数生成:使用Java的Random类来生成随机数,以实现掷骰子的效果。 输入输出:使用Java的Scanner类来进行输入、使用System.out.println()方法来进行输出。 数值类型:将投掷骰子得到的点数以及玩家的得分定义为int类型,以存储整数数值。