- Python 版本hailiangwang.com
Python 版本的“猜数字”游戏非常直观:
python
import random
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print(“欢迎来到猜数字游戏!我已经选了一个1到100之间的数字。”)
print(“请猜猜是哪个数字:”)
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(“请输入一个有效的数字。”)
2. JavaScript 版本(用于网页)
这个版本的“猜数字”游戏使用HTML和JavaScript来实现:
HTML (index.html):
html
猜数字游戏
我已经想好了一个1到100之间的数字,你能猜到是多少吗?
提交 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);
attempts++;
if (guess < numberToGuess) {
feedback.textContent = "太小了!再试试看。";
} else if (guess > numberToGuess) {
feedback.textContent = "太大了!再试试看。";
} else {
feedback.textContent = `恭喜你!答对了!数字就是${numberToGuess}。你尝试了${attempts}次。`;
}
}
3. C++ 版本
C++ 版本的“猜数字”游戏在控制台中运行:
cpp
#include
#include // 为了使用rand()和srand()
#include // 为了使用time()
using namespace std;
int main() {
srand(time(0)); // 初始化随机数生成器
int numberToGuess = rand() % 100 + 1; // 生成1到100之间的随机数
int guess = 0;
int attempts = 0;
cout << "欢迎来到猜数字游戏!我已经选了一个1到100之间的数字。" << endl;
cout << "请猜猜是哪个数字:" << endl;
while (guess != numberToGuess) {
cin >> guess;
attempts++;
if (guess < numberToGuess) {
cout << "太小了!再试试看。" << endl;
} else if (guess > numberToGuess) {
cout << "太大了!再试试看。" << endl;
} else {
cout << "恭喜你!答对了!数字就是" << numberToGuess << "。你尝试了" << attempts << "次。" << endl;
}
}
return 0;
}
以上就是用Python、JavaScript(用于网页)和C++实现“猜数字”游戏的简单示例。由于篇幅限制,我将为你提供一个简单的“猜数字”游戏示例,分别用Python、JavaScript(用于网页)和C++(控制台应用)来实现。
- Python 版本
Python 版本的“猜数字”游戏非常直观:
python
import random
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print(“欢迎来到猜数字游戏!我已经选了一个1到100之间的数字。”)
print(“请猜猜是哪个数字:”)
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(“请输入一个有效的数字。”)
2. JavaScript 版本(用于网页)
这个版本的“猜数字”游戏使用HTML和JavaScript来实现:
HTML (index.html):
html
猜数字游戏
我已经想好了一个1到100之间的数字,你能猜到是多少吗?
提交 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);
attempts++;
if (guess < numberToGuess) {
feedback.textContent = "太小了!再试试看。";
} else if (guess > numberToGuess) {
feedback.textContent = "太大了!再试试看。";
} else {
feedback.textContent = `恭喜你!答对了!数字就是${numberToGuess}。你尝试了${attempts}次。`;
}
}
3. C++ 版本
C++ 版本的“猜数字”游戏在控制台中运行:
cpp
#include
#include // 为了使用rand()和srand()
#include // 为了使用time()
using namespace std;
int main() {
srand(time(0)); // 初始化随机数生成器
int numberToGuess = rand() % 100 + 1; // 生成1到100之间的随机数
int guess = 0;
int attempts = 0;
cout << "欢迎来到猜数字游戏!我已经选了一个1到100之间的数字。" << endl;
cout << "请猜猜是哪个数字:" << endl;
while (guess != numberToGuess) {
cin >> guess;
attempts++;
if (guess < numberToGuess) {
cout << "太小了!再试试看。" << endl;
} else if (guess > numberToGuess) {
cout << "太大了!再试试看。" << endl;
} else {
cout << "恭喜你!答对了!数字就是" << numberToGuess << "。你尝试了" << attempts << "次。" << endl;
}
}
return 0;
}
以上就是用Python、JavaScript(用于网页)和C++实现“猜数字”游戏的简单示例。