- Python 版本gwygov.cn
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 版本(用于网页)
JavaScript版本会稍微复杂一些,因为它需要HTML来显示界面。
HTML (index.html):
html
猜数字游戏
我有一个1到100之间的数字,你能猜到是哪个吗?
提交 JavaScript (game.js):javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function checkGuess() {
guess = parseInt(document.getElementById(‘guess’).value);
attempts++;
let feedback = document.getElementById(‘feedback’);
if (guess === numberToGuess) {
feedback.textContent = `恭喜你!答案就是${numberToGuess}。你总共尝试了${attempts}次。`;
} else if (guess < numberToGuess) {
feedback.textContent = "太低了!";
} else {
feedback.textContent = "太高了!";
}
}
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;
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版本非常直观,适合初学者。
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 版本(用于网页)
JavaScript版本会稍微复杂一些,因为它需要HTML来显示界面。
HTML (index.html):
html
猜数字游戏
我有一个1到100之间的数字,你能猜到是哪个吗?
提交 JavaScript (game.js):javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function checkGuess() {
guess = parseInt(document.getElementById(‘guess’).value);
attempts++;
let feedback = document.getElementById(‘feedback’);
if (guess === numberToGuess) {
feedback.textContent = `恭喜你!答案就是${numberToGuess}。你总共尝试了${attempts}次。`;
} else if (guess < numberToGuess) {
feedback.textContent = "太低了!";
} else {
feedback.textContent = "太高了!";
}
}
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;
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;
}
这三个示例展示了如何使用不同的编程语言来实现一个简单的“猜数字”游戏。每种语言都有其独特的语法和特性,但基本概念是相似的。