Python 示例
python
import random
def guess_number_game(): dz61.com
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("请输入一个有效的数字。")
guess_number_game()
JavaScript 示例(HTML + JS)
HTML部分:
html
猜数字游戏
我有一个1到100之间的数字。你能猜到是哪个吗?
猜一猜 JavaScript部分 (game.js):javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
let guess;
function checkGuess() {
guess = document.getElementById(‘guess’).value;
attempts++;
let feedback = document.getElementById(‘feedback’);
if (guess < numberToGuess) {
feedback.textContent = "太低了!";
} else if (guess > numberToGuess) {
feedback.textContent = "太高了!";
} else {
feedback.textContent = `恭喜你!答对了!你总共猜了${attempts}次。`;
}
}
C++ 示例
C++版本通常涉及更多输入输出的复杂性,这里提供一个简化的控制台版本:
cpp
#include
#include // For rand() and srand()
#include // For time()
using namespace std;
int main() {
srand(time(0)); // Seed random number generator
int numberToGuess = rand() % 100 + 1;
int guess, attempts = 0;
cout << "我想了一个1到100之间的数字。试试看能不能猜到它!" << endl;
do {
cout << "你的猜测是:";
cin >> guess;
attempts++;
if (guess < numberToGuess) {
cout << "太低了!" << endl;
} else if (guess > numberToGuess) {
cout << "太高了!" << endl;
}
} while (guess != numberToGuess);
cout << "恭喜你!答对了!你总共猜了" << attempts << "次。" << endl;
return 0;
}
以上是用Python、JavaScript(结合HTML)和C++编写的“猜数字”游戏的基本实现。每种语言都有其特定的语法和库来处理输入输出及随机数生成等任务。由于篇幅限制,我将为你提供一个简单的“猜数字”游戏的示例,用几种不同的编程语言来实现。这个游戏的基本逻辑是:程序随机生成一个数字(例如1到100之间),玩家尝试猜测这个数字,程序会告诉玩家猜测的数字是太高、太低还是正确。
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("请输入一个有效的数字。")
guess_number_game()
JavaScript 示例(HTML + JS)
HTML部分:
html
猜数字游戏
我有一个1到100之间的数字。你能猜到是哪个吗?
猜一猜 JavaScript部分 (game.js):javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
let guess;
function checkGuess() {
guess = document.getElementById(‘guess’).value;
attempts++;
let feedback = document.getElementById(‘feedback’);
if (guess < numberToGuess) {
feedback.textContent = "太低了!";
} else if (guess > numberToGuess) {
feedback.textContent = "太高了!";
} else {
feedback.textContent = `恭喜你!答对了!你总共猜了${attempts}次。`;
}
}
C++ 示例
C++版本通常涉及更多输入输出的复杂性,这里提供一个简化的控制台版本:
cpp
#include
#include // For rand() and srand()
#include // For time()
using namespace std;
int main() {
srand(time(0)); // Seed random number generator
int numberToGuess = rand() % 100 + 1;
int guess, attempts = 0;
cout << "我想了一个1到100之间的数字。试试看能不能猜到它!" << endl;
do {
cout << "你的猜测是:";
cin >> guess;
attempts++;
if (guess < numberToGuess) {
cout << "太低了!" << endl;
} else if (guess > numberToGuess) {
cout << "太高了!" << endl;
}
} while (guess != numberToGuess);
cout << "恭喜你!答对了!你总共猜了" << attempts << "次。" << endl;
return 0;
}
以上是用Python、JavaScript(结合HTML)和C++编写的“猜数字”游戏的基本实现。每种语言都有其特定的语法和库来处理输入输出及随机数生成等任务。