- Python 版本chanpang888.com
Python 版本的猜数字游戏非常直观:
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("猜猜看,我心里想的是哪个1到100之间的数字?")
while guess != number_to_guess:
try:
guess = int(input("请输入你的猜测:"))
attempts += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
else:
print(f"恭喜你,猜对了!数字就是{number_to_guess}。你尝试了{attempts}次。")
except ValueError:
print("请输入一个有效的数字。")
if name == “main”:
guess_number_game()
2. JavaScript (HTML + JavaScript) 版本
HTML 部分(index.html):
html
猜猜看,我心里想的是哪个数字?(1-100)
提交<script src="game.js"></script>
JavaScript 部分(game.js):
javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
let feedback = document.getElementById(‘feedback’);
function checkGuess() {
guess = parseInt(document.getElementById(‘guess’).value, 10);
attempts++;
if (isNaN(guess)) {
feedback.textContent = "请输入一个有效的数字。";
return;
}
if (guess < numberToGuess) {
feedback.textContent = "太小了!";
} else if (guess > numberToGuess) {
feedback.textContent = "太大了!";
} else {
feedback.textContent = `恭喜你,猜对了!数字就是${numberToGuess}。你尝试了${attempts}次。`;
}
}
3. C++ 版本
C++ 版本的游戏需要用户通过控制台输入,这里使用 和 :
cpp
#include
#include
#include
using namespace std;
int main() {
srand(time(0)); // 初始化随机数种子
int numberToGuess = rand() % 100 + 1;
int guess;
int attempts = 0;
cout << "猜猜看,我心里想的是哪个1到100之间的数字?" << endl;
while (true) {
cin >> guess;
attempts++;
if (guess < numberToGuess) {
cout << "太小了!" << endl;
} else if (guess > numberToGuess) {
cout << "太大了!" << endl;
} else {
cout << "恭喜你,猜对了!数字就是" << numberToGuess << "。你尝试了" << attempts << "次。" << endl;
break;
}
}
return 0;
}
这三个示例展示了如何用不同的编程语言实现一个简单的猜数字游戏。每个示例都遵循了基本的游戏规则和逻辑,但语言间的语法和特性有所不同。由于直接提供完整的游戏代码可能过于复杂且依赖于具体的游戏类型和规则,我将为你提供一个简单的猜数字游戏的概念性实现,分别用Python、JavaScript(用于网页)、和C++这三种流行的编程语言来演示。
- Python 版本
Python 版本的猜数字游戏非常直观:
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("猜猜看,我心里想的是哪个1到100之间的数字?")
while guess != number_to_guess:
try:
guess = int(input("请输入你的猜测:"))
attempts += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
else:
print(f"恭喜你,猜对了!数字就是{number_to_guess}。你尝试了{attempts}次。")
except ValueError:
print("请输入一个有效的数字。")
if name == “main”:
guess_number_game()
2. JavaScript (HTML + JavaScript) 版本
HTML 部分(index.html):
html
猜猜看,我心里想的是哪个数字?(1-100)
提交<script src="game.js"></script>
JavaScript 部分(game.js):
javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
let feedback = document.getElementById(‘feedback’);
function checkGuess() {
guess = parseInt(document.getElementById(‘guess’).value, 10);
attempts++;
if (isNaN(guess)) {
feedback.textContent = "请输入一个有效的数字。";
return;
}
if (guess < numberToGuess) {
feedback.textContent = "太小了!";
} else if (guess > numberToGuess) {
feedback.textContent = "太大了!";
} else {
feedback.textContent = `恭喜你,猜对了!数字就是${numberToGuess}。你尝试了${attempts}次。`;
}
}
3. C++ 版本
C++ 版本的游戏需要用户通过控制台输入,这里使用 和 :
cpp
#include
#include
#include
using namespace std;
int main() {
srand(time(0)); // 初始化随机数种子
int numberToGuess = rand() % 100 + 1;
int guess;
int attempts = 0;
cout << "猜猜看,我心里想的是哪个1到100之间的数字?" << endl;
while (true) {
cin >> guess;
attempts++;
if (guess < numberToGuess) {
cout << "太小了!" << endl;
} else if (guess > numberToGuess) {
cout << "太大了!" << endl;
} else {
cout << "恭喜你,猜对了!数字就是" << numberToGuess << "。你尝试了" << attempts << "次。" << endl;
break;
}
}
return 0;
}
这三个示例展示了如何用不同的编程语言实现一个简单的猜数字游戏。每个示例都遵循了基本的游戏规则和逻辑,但语言间的语法和特性有所不同。