Python
python
import random
def game(): 33066.cn
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
guess = int(input("猜一个 1 到 100 之间的数字: "))
attempts += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
print(f"恭喜你,猜对了! 你尝试了 {attempts} 次。")
if name == “main”:
game()
JavaScript (在浏览器控制台或 HTML 文件中运行)
HTML 文件 (game.html):
html
<script>
function startGame() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function getGuess() {
return parseInt(prompt("猜一个 1 到 100 之间的数字:"));
}
function checkGuess() {
guess = getGuess();
attempts++;
if (guess < numberToGuess) {
return "太小了!";
} else if (guess > numberToGuess) {
return "太大了!";
} else {
return `恭喜你,猜对了! 你尝试了 ${attempts} 次。`;
}
}
let message = checkGuess();
while (message.includes("太")) {
message = checkGuess();
}
document.getElementById("output").textContent = message;
}
</script>
C++ (使用命令行) cpp #include #include #include
int main() {
std::srand(std::time(0)); // 使用当前时间作为随机数生成器的种子
int numberToGuess = std::rand() % 100 + 1;
int guess = 0;
int attempts = 0;
std::cout << "猜一个 1 到 100 之间的数字: ";
while (guess != numberToGuess) {
std::cin >> guess;
attempts++;
if (guess < numberToGuess) {
std::cout << "太小了!\n";
} else if (guess > numberToGuess) {
std::cout << "太大了!\n";
}
std::cout << "再试一次: ";
}
std::cout << "恭喜你,猜对了! 你尝试了 " << attempts << " 次。" << std::endl;
return 0;
}
请注意,这些代码示例只是为了演示目的,并且可能需要进行一些修改以满足您的具体需求。由于篇幅限制,我无法为所有编程语言都提供完整的游戏代码,但我可以为几种常见的编程语言(如 Python、JavaScript 和 C++)提供简单的“猜数字”游戏的示例代码。
Python
python
import random
def game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
guess = int(input("猜一个 1 到 100 之间的数字: "))
attempts += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
print(f"恭喜你,猜对了! 你尝试了 {attempts} 次。")
if name == “main”:
game()
JavaScript (在浏览器控制台或 HTML 文件中运行)
HTML 文件 (game.html):
html
<script>
function startGame() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function getGuess() {
return parseInt(prompt("猜一个 1 到 100 之间的数字:"));
}
function checkGuess() {
guess = getGuess();
attempts++;
if (guess < numberToGuess) {
return "太小了!";
} else if (guess > numberToGuess) {
return "太大了!";
} else {
return `恭喜你,猜对了! 你尝试了 ${attempts} 次。`;
}
}
let message = checkGuess();
while (message.includes("太")) {
message = checkGuess();
}
document.getElementById("output").textContent = message;
}
</script>
C++ (使用命令行) cpp #include #include #include
int main() {
std::srand(std::time(0)); // 使用当前时间作为随机数生成器的种子
int numberToGuess = std::rand() % 100 + 1;
int guess = 0;
int attempts = 0;
std::cout << "猜一个 1 到 100 之间的数字: ";
while (guess != numberToGuess) {
std::cin >> guess;
attempts++;
if (guess < numberToGuess) {
std::cout << "太小了!\n";
} else if (guess > numberToGuess) {
std::cout << "太大了!\n";
}
std::cout << "再试一次: ";
}
std::cout << "恭喜你,猜对了! 你尝试了 " << attempts << " 次。" << std::endl;
return 0;
}
请注意,这些代码示例只是为了演示目的,并且可能需要进行一些修改以满足您的具体需求。