- Python 版本的猜数字游戏gzjiazheng.cn
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"恭喜你!答对了。你尝试了{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;
function checkGuess() {
guess = parseInt(document.getElementById(‘guessInput’).value, 10);
attempts++;
let feedback = document.getElementById('feedback');
if (isNaN(guess)) {
feedback.textContent = "请输入一个有效的数字。";
} else if (guess < numberToGuess) {
feedback.textContent = "太小了!";
} else if (guess > numberToGuess) {
feedback.textContent = "太大了!";
} else {
feedback.textContent = `恭喜你!答对了。你尝试了${attempts}次。`;
}
}
3. C++ 版本的猜数字游戏(控制台)
cpp
#include
#include // 用于rand()和srand()
#include // 用于time()
using namespace std;
int main() {
srand(static_cast(time(0))); // 初始化随机数种子
int numberToGuess = rand() % 100 + 1;
int guess;
int attempts = 0;
cout << "猜数字游戏!我已经选了一个1到100之间的数字。" << endl;
while (cin >> guess && guess != numberToGuess) {
attempts++;
if (guess < numberToGuess) {
cout << "太小了!" << endl;
} else if (guess > numberToGuess) {
cout << "太大了!" << endl;
}
}
cout << "恭喜你!答对了。你尝试了" << attempts << "次。" << endl;
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("猜数字游戏!我已经选了一个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"恭喜你!答对了。你尝试了{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;
function checkGuess() {
guess = parseInt(document.getElementById(‘guessInput’).value, 10);
attempts++;
let feedback = document.getElementById('feedback');
if (isNaN(guess)) {
feedback.textContent = "请输入一个有效的数字。";
} else if (guess < numberToGuess) {
feedback.textContent = "太小了!";
} else if (guess > numberToGuess) {
feedback.textContent = "太大了!";
} else {
feedback.textContent = `恭喜你!答对了。你尝试了${attempts}次。`;
}
}
3. C++ 版本的猜数字游戏(控制台)
cpp
#include
#include // 用于rand()和srand()
#include // 用于time()
using namespace std;
int main() {
srand(static_cast(time(0))); // 初始化随机数种子
int numberToGuess = rand() % 100 + 1;
int guess;
int attempts = 0;
cout << "猜数字游戏!我已经选了一个1到100之间的数字。" << endl;
while (cin >> guess && guess != numberToGuess) {
attempts++;
if (guess < numberToGuess) {
cout << "太小了!" << endl;
} else if (guess > numberToGuess) {
cout << "太大了!" << endl;
}
}
cout << "恭喜你!答对了。你尝试了" << attempts << "次。" << endl;
return 0;
}
以上三种语言的代码均实现了相同的猜数字游戏逻辑,但分别适用于不同的编程环境和场景。