- Python版
Python是一种非常适合编写小型游戏和脚本的语言。
python
import random
def guess_number_game(): timezf.cn
number_to_guess = random.randint(1, 100)
guess = None
tries = 0
print("猜数字游戏!我已经想好了一个1到100之间的数字。")
while guess != number_to_guess:
try:
guess = int(input("请输入你猜的数字:"))
tries += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
else:
print(f"恭喜你!答对了!数字是{number_to_guess}。你尝试了{tries}次。")
except ValueError:
print("请输入一个有效的数字!")
if name == “main”:
guess_number_game()
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 tries = 0;
function checkGuess() {
guess = parseInt(document.getElementById(‘guess’).value);
tries++;
let feedback = document.getElementById(‘feedback’);
if (guess < numberToGuess) {
feedback.textContent = "太小了!";
} else if (guess > numberToGuess) {
feedback.textContent = "太大了!";
} else {
feedback.textContent = `恭喜你!答对了!数字是${numberToGuess}。你尝试了${tries}次。`;
}
}
3. C++版
C++版本通常用于控制台应用。
cpp
#include
#include
#include
int main() {
srand(time(0));
int numberToGuess = rand() % 100 + 1;
int guess = 0;
int tries = 0;
std::cout << "猜数字游戏!我已经想好了一个1到100之间的数字。" << std::endl;
while (guess != numberToGuess) {
std::cout << "请输入你猜的数字:";
std::cin >> guess;
tries++;
if (guess < numberToGuess) {
std::cout << "太小了!" << std::endl;
} else if (guess > numberToGuess) {
std::cout << "太大了!" << std::endl;
} else {
std::cout << "恭喜你!答对了!数字是" << numberToGuess << "。你尝试了" << tries << "次。" << std::endl;
}
}
return 0;
}
这些示例提供了“猜数字”游戏的基本框架,你可以根据需要进行扩展和修改。由于篇幅限制,我将为你提供一个简单的“猜数字”游戏的基本框架,分别用Python、JavaScript(针对网页)和C++来实现。
- Python版
Python是一种非常适合编写小型游戏和脚本的语言。
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
tries = 0
print("猜数字游戏!我已经想好了一个1到100之间的数字。")
while guess != number_to_guess:
try:
guess = int(input("请输入你猜的数字:"))
tries += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
else:
print(f"恭喜你!答对了!数字是{number_to_guess}。你尝试了{tries}次。")
except ValueError:
print("请输入一个有效的数字!")
if name == “main”:
guess_number_game()
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 tries = 0;
function checkGuess() {
guess = parseInt(document.getElementById(‘guess’).value);
tries++;
let feedback = document.getElementById(‘feedback’);
if (guess < numberToGuess) {
feedback.textContent = "太小了!";
} else if (guess > numberToGuess) {
feedback.textContent = "太大了!";
} else {
feedback.textContent = `恭喜你!答对了!数字是${numberToGuess}。你尝试了${tries}次。`;
}
}
3. C++版
C++版本通常用于控制台应用。
cpp
#include
#include
#include
int main() {
srand(time(0));
int numberToGuess = rand() % 100 + 1;
int guess = 0;
int tries = 0;
std::cout << "猜数字游戏!我已经想好了一个1到100之间的数字。" << std::endl;
while (guess != numberToGuess) {
std::cout << "请输入你猜的数字:";
std::cin >> guess;
tries++;
if (guess < numberToGuess) {
std::cout << "太小了!" << std::endl;
} else if (guess > numberToGuess) {
std::cout << "太大了!" << std::endl;
} else {
std::cout << "恭喜你!答对了!数字是" << numberToGuess << "。你尝试了" << tries << "次。" << std::endl;
}
}
return 0;
}
这些示例提供了“猜数字”游戏的基本框架,你可以根据需要进行扩展和修改。