- Python 版本的猜数字游戏maoxianjiu.cn
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
attempts = 0
print(“欢迎来到猜数字游戏!”)
print(“我已经想好了一个1到100之间的数字。”)
print(“你能猜到是哪个数字吗?”)
while True:
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}次。")
break
except ValueError:
print("请输入一个有效的数字。")
if name == “main”:
guess_number_game()
2. JavaScript 版本的猜数字游戏(网页)
html
猜数字游戏
我已经想好了一个1到100之间的数字,你能猜到是哪个吗?
提交<script>
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
function checkGuess() {
const guess = parseInt(document.getElementById('guess').value);
attempts++;
const resultElement = document.getElementById('result');
if (guess < numberToGuess) {
resultElement.innerText = "太小了!";
} else if (guess > numberToGuess) {
resultElement.innerText = "太大了!";
} else {
resultElement.innerText = `恭喜你!答对了!数字就是${numberToGuess}。你总共尝试了${attempts}次。`;
}
}
</script>
3. C++ 版本的猜数字游戏(控制台) cpp #include #include #include
using namespace std;
int main() {
srand(time(0)); // 初始化随机数种子
int numberToGuess = rand() % 100 + 1;
int guess, 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 << "恭喜你!答对了!数字就是" << numberToGuess << "。" << endl;
cout << "你总共尝试了" << attempts << "次。" << endl;
break;
}
}
return 0;
}
这三个示例分别展示了如何用Python、JavaScript(网页版)和C++来编写一个简单的猜数字游戏。每种语言都有其独特的语法和库,但基本逻辑是相似的。由于篇幅限制,我将为你提供一个简单的猜数字游戏示例,分别用Python、JavaScript(适用于网页)和C++编写。
- Python 版本的猜数字游戏
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
attempts = 0
print(“欢迎来到猜数字游戏!”)
print(“我已经想好了一个1到100之间的数字。”)
print(“你能猜到是哪个数字吗?”)
while True:
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}次。")
break
except ValueError:
print("请输入一个有效的数字。")
if name == “main”:
guess_number_game()
2. JavaScript 版本的猜数字游戏(网页)
html
猜数字游戏
我已经想好了一个1到100之间的数字,你能猜到是哪个吗?
提交<script>
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
function checkGuess() {
const guess = parseInt(document.getElementById('guess').value);
attempts++;
const resultElement = document.getElementById('result');
if (guess < numberToGuess) {
resultElement.innerText = "太小了!";
} else if (guess > numberToGuess) {
resultElement.innerText = "太大了!";
} else {
resultElement.innerText = `恭喜你!答对了!数字就是${numberToGuess}。你总共尝试了${attempts}次。`;
}
}
</script>
3. C++ 版本的猜数字游戏(控制台) cpp #include #include #include
using namespace std;
int main() {
srand(time(0)); // 初始化随机数种子
int numberToGuess = rand() % 100 + 1;
int guess, 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 << "恭喜你!答对了!数字就是" << numberToGuess << "。" << endl;
cout << "你总共尝试了" << attempts << "次。" << endl;
break;
}
}
return 0;
}
这三个示例分别展示了如何用Python、JavaScript(网页版)和C++来编写一个简单的猜数字游戏。每种语言都有其独特的语法和库,但基本逻辑是相似的。