- Python 版本earnersclub247.com
Python 是一种广泛使用的高级编程语言,非常适合快速开发。
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("欢迎来到猜数字游戏!")
print("我已经想好了一个1到100之间的数字。")
print("试试看,你能不能猜到它。")
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/JS)
JavaScript 通常在网页上运行,与HTML和CSS一起工作。
html
猜数字游戏
我已经想好了一个1到100之间的数字。试试看,你能不能猜到它。
提交<script>
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 = "太小了!";
} else if (guess > numberToGuess) {
feedback.textContent = "太大了!";
} else {
feedback.textContent = `恭喜你!答案正确。你总共猜了${attempts}次。`;
}
}
</script>
3. C++ 版本
C++ 是一种编译型语言,需要编译器来运行。
cpp
#include
#include
#include
using namespace std;
int main() {
srand(time(0)); // 初始化随机数种子
int numberToGuess = rand() % 100 + 1;
int guess;
int attempts = 0;
cout << "欢迎来到猜数字游戏!" << endl;
cout << "我已经想好了一个1到100之间的数字。" << endl;
cout << "试试看,你能不能猜到它。" << endl;
while (true) {
cout << "请输入你的猜测:";
cin >> guess;
attempts++;
if (guess < numberToGuess) {
cout << "太小了!" << endl;
} else if (guess > numberToGuess) {
cout << "太大了!" << endl;
} else {
cout << "恭喜你!答案正确。你总共猜了" << attempts << "次。" << endl;
break;
}
}
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之间的数字。")
print("试试看,你能不能猜到它。")
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/JS)
JavaScript 通常在网页上运行,与HTML和CSS一起工作。
html
猜数字游戏
我已经想好了一个1到100之间的数字。试试看,你能不能猜到它。
提交<script>
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 = "太小了!";
} else if (guess > numberToGuess) {
feedback.textContent = "太大了!";
} else {
feedback.textContent = `恭喜你!答案正确。你总共猜了${attempts}次。`;
}
}
</script>
3. C++ 版本
C++ 是一种编译型语言,需要编译器来运行。
cpp
#include
#include
#include
using namespace std;
int main() {
srand(time(0)); // 初始化随机数种子
int numberToGuess = rand() % 100 + 1;
int guess;
int attempts = 0;
cout << "欢迎来到猜数字游戏!" << endl;
cout << "我已经想好了一个1到100之间的数字。" << endl;
cout << "试试看,你能不能猜到它。" << endl;
while (true) {
cout << "请输入你的猜测:";
cin >> guess;
attempts++;
if (guess < numberToGuess) {
cout << "太小了!" << endl;
} else if (guess > numberToGuess) {
cout << "太大了!" << endl;
} else {
cout << "恭喜你!答案正确。你总共猜了" << attempts << "次。" << endl;
break;
}
}
return 0;
}
以上示例展示了如何用不同编程语言实现一个简单的猜数字游戏。每种语言都有其特定的语法和库,但基本的逻辑和算法是相似的。