
- Python版本aiermaweiyu.com
Python版本的“猜数字”游戏简单直观。
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("欢迎来到猜数字游戏!")
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"恭喜你!答案就是{number_to_guess}。")
print(f"你总共猜了{attempts}次。")
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 attempts = 0;
let feedback = document.getElementById(‘feedback’);
function checkGuess() {
guess = parseInt(document.getElementById(‘guess’).value, 10);
attempts++;
if (isNaN(guess)) {
feedback.textContent = "请输入一个有效的数字。";
return;
}
if (guess < numberToGuess) {
feedback.textContent = "太小了!";
} else if (guess > numberToGuess) {
feedback.textContent = "太大了!";
} else {
feedback.textContent = `恭喜你!答案就是${numberToGuess}。你总共猜了${attempts}次。`;
}
}
3. C++版本
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;
do {
cout << "请猜一个数字:";
cin >> guess;
attempts++;
if (guess < numberToGuess) {
cout << "太小了!" << endl;
} else if (guess > numberToGuess) {
cout << "太大了!" << endl;
}
} while (guess != numberToGuess);
cout << "恭喜你!答案就是" << numberToGuess << "。你总共猜了" << attempts << "次。" << endl;
return 0;
}
这三个示例展示了如何用不同编程语言实现一个简单的“猜数字”游戏。你可以根据自己的需要和环境选择适合的版本。由于篇幅限制,我将为你提供一个简单的“猜数字”游戏示例,分别用Python、JavaScript(在Web环境中运行)和C++三种编程语言来实现。
- Python版本
Python版本的“猜数字”游戏简单直观。
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("欢迎来到猜数字游戏!")
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"恭喜你!答案就是{number_to_guess}。")
print(f"你总共猜了{attempts}次。")
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 attempts = 0;
let feedback = document.getElementById(‘feedback’);
function checkGuess() {
guess = parseInt(document.getElementById(‘guess’).value, 10);
attempts++;
if (isNaN(guess)) {
feedback.textContent = "请输入一个有效的数字。";
return;
}
if (guess < numberToGuess) {
feedback.textContent = "太小了!";
} else if (guess > numberToGuess) {
feedback.textContent = "太大了!";
} else {
feedback.textContent = `恭喜你!答案就是${numberToGuess}。你总共猜了${attempts}次。`;
}
}
3. C++版本
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;
do {
cout << "请猜一个数字:";
cin >> guess;
attempts++;
if (guess < numberToGuess) {
cout << "太小了!" << endl;
} else if (guess > numberToGuess) {
cout << "太大了!" << endl;
}
} while (guess != numberToGuess);
cout << "恭喜你!答案就是" << numberToGuess << "。你总共猜了" << attempts << "次。" << endl;
return 0;
}
这三个示例展示了如何用不同编程语言实现一个简单的“猜数字”游戏。你可以根据自己的需要和环境选择适合的版本。
和C++三种编程语言来实现。&spm=1001.2101.3001.5002&articleId=140354917&d=1&t=3&u=c010297713194727adf37018b30adc10)
993

被折叠的 条评论
为什么被折叠?



