- Python 版本的猜数字游戏
python
import random
def guess_number_game(): wandarealmwuhan.cn
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("欢迎来到猜数字游戏!")
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}。")
print(f"你总共尝试了 {attempts} 次。")
except ValueError:
print("请输入一个有效的数字。")
if name == “main”:
guess_number_game()
2. 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;
function guess() {
guess = parseInt(document.getElementById(‘guessInput’).value, 10);
attempts++;
if (isNaN(guess)) {
alert("请输入一个有效的数字。");
return;
}
let resultElement = document.getElementById('result');
resultElement.innerText = '';
if (guess < numberToGuess) {
resultElement.innerText = "太小了!";
} else if (guess > numberToGuess) {
resultElement.innerText = "太大了!";
} else {
resultElement.innerText = `恭喜你猜对了!数字就是 ${numberToGuess}。你总共尝试了 ${attempts} 次。`;
}
}
3. C++ 版本的猜数字游戏(控制台)
cpp
#include
#include // For rand() and srand()
#include // For time()
using namespace std;
int main() {
srand(time(0)); // Seed the random number generator
int numberToGuess = rand() % 100 + 1;
int guess;
int attempts = 0;
cout << "欢迎来到猜数字游戏!" << endl;
cout << "我已经想好了一个1到100之间的数字。" << endl;
cout << "你能猜到是哪个数字吗?" << endl;
while (true) {
cout << "请输入你的猜测:";
cin >> guess;
attempts++;
if (guess < numberToGuess) {
cout << "太小了!" << endl;
} else if (guess > numberToGuess) {
cout << "太大了!" << endl;
} else {
cout << "恭喜你猜对了!数字就是 " << numberToGuess << "。" << endl;
cout << "你总共尝试了 " << attempts << " 次。" << endl;
break;
}
}
return 0;
}
这些示例提供了三种不同编程环境中实现简单猜数字游戏的方法。希望对你有所帮助!由于篇幅限制,我将为你提供一个简单的“猜数字”游戏示例,分别用Python、JavaScript(用于网页)和C++三种编程语言实现。
- Python 版本的猜数字游戏
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("欢迎来到猜数字游戏!")
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}。")
print(f"你总共尝试了 {attempts} 次。")
except ValueError:
print("请输入一个有效的数字。")
if name == “main”:
guess_number_game()
2. 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;
function guess() {
guess = parseInt(document.getElementById(‘guessInput’).value, 10);
attempts++;
if (isNaN(guess)) {
alert("请输入一个有效的数字。");
return;
}
let resultElement = document.getElementById('result');
resultElement.innerText = '';
if (guess < numberToGuess) {
resultElement.innerText = "太小了!";
} else if (guess > numberToGuess) {
resultElement.innerText = "太大了!";
} else {
resultElement.innerText = `恭喜你猜对了!数字就是 ${numberToGuess}。你总共尝试了 ${attempts} 次。`;
}
}
3. C++ 版本的猜数字游戏(控制台)
cpp
#include
#include // For rand() and srand()
#include // For time()
using namespace std;
int main() {
srand(time(0)); // Seed the random number generator
int numberToGuess = rand() % 100 + 1;
int guess;
int attempts = 0;
cout << "欢迎来到猜数字游戏!" << endl;
cout << "我已经想好了一个1到100之间的数字。" << endl;
cout << "你能猜到是哪个数字吗?" << endl;
while (true) {
cout << "请输入你的猜测:";
cin >> guess;
attempts++;
if (guess < numberToGuess) {
cout << "太小了!" << endl;
} else if (guess > numberToGuess) {
cout << "太大了!" << endl;
} else {
cout << "恭喜你猜对了!数字就是 " << numberToGuess << "。" << endl;
cout << "你总共尝试了 " << attempts << " 次。" << endl;
break;
}
}
return 0;
}
这些示例提供了三种不同编程环境中实现简单猜数字游戏的方法。希望对你有所帮助!