- Python 版本hezhongliancai.com
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. C++ 版本
C++ 版本的实现稍微复杂一些,因为需要处理输入输出和异常。这里仅展示核心逻辑:
cpp
#include
#include // For rand() and srand()
#include // For time()
int main() {
srand(time(0)); // Seed the random number generator
int number_to_guess = rand() % 100 + 1;
int guess;
int attempts = 0;
std::cout << "欢迎来到猜数字游戏!\n";
std::cout << "我已经想好了一个1到100之间的数字。\n";
while (true) {
std::cout << "请猜一个数字:";
std::cin >> guess;
attempts++;
if (std::cin.fail()) {
std::cin.clear(); // Clear error flags
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Ignore rest of line
std::cout << "请输入一个有效的数字!\n";
continue;
}
if (guess < number_to_guess) {
std::cout << "太小了!\n";
} else if (guess > number_to_guess) {
std::cout << "太大了!\n";
} else {
std::cout << "恭喜你!答案就是" << number_to_guess << "。\n";
std::cout << "你总共猜了" << attempts << "次。\n";
break;
}
}
return 0;
}
3. JavaScript 版本(Node.js 环境)
javascript
const readline = require(‘readline’).createInterface({
input: process.stdin,
output: process.stdout
});
function guessNumberGame() {
const numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
console.log(“欢迎来到猜数字游戏!”);
console.log(“我已经想好了一个1到100之间的数字。”);
function promptForGuess() {
readline.question(‘请猜一个数字:’, (input) => {
guess = parseInt(input, 10);
attempts++;
if (isNaN(guess)) {
console.log("请输入一个有效的数字!");
promptForGuess(); // Recursive call for invalid input
} else if (guess < numberToGuess) {
console.log("太小了!");
promptForGuess();
} else if (guess > numberToGuess) {
console.log("太大了!");
promptForGuess();
} else {
console.log(`恭喜你!答案就是${numberToGuess}。`);
console.log(`你总共猜了${attempts}次。`);
readline.close();
}
});
}
promptForGuess();
}
guessNumberGame();
以上是用三种不同编程语言实现的简单猜数字游戏的基本框架。每种语言都有其独特的语法和库来处理输入输出、随机数生成由于直接展示多种编程语言的全套游戏代码可能过于冗长,我将分别用几种流行的编程语言(Python、C++、JavaScript)概述一个简单的猜数字游戏的基本框架。这个游戏将让玩家尝试猜测一个由程序随机生成的数字。
- 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. C++ 版本
C++ 版本的实现稍微复杂一些,因为需要处理输入输出和异常。这里仅展示核心逻辑:
cpp
#include
#include // For rand() and srand()
#include // For time()
int main() {
srand(time(0)); // Seed the random number generator
int number_to_guess = rand() % 100 + 1;
int guess;
int attempts = 0;
std::cout << "欢迎来到猜数字游戏!\n";
std::cout << "我已经想好了一个1到100之间的数字。\n";
while (true) {
std::cout << "请猜一个数字:";
std::cin >> guess;
attempts++;
if (std::cin.fail()) {
std::cin.clear(); // Clear error flags
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Ignore rest of line
std::cout << "请输入一个有效的数字!\n";
continue;
}
if (guess < number_to_guess) {
std::cout << "太小了!\n";
} else if (guess > number_to_guess) {
std::cout << "太大了!\n";
} else {
std::cout << "恭喜你!答案就是" << number_to_guess << "。\n";
std::cout << "你总共猜了" << attempts << "次。\n";
break;
}
}
return 0;
}
3. JavaScript 版本(Node.js 环境)
javascript
const readline = require(‘readline’).createInterface({
input: process.stdin,
output: process.stdout
});
function guessNumberGame() {
const numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
console.log(“欢迎来到猜数字游戏!”);
console.log(“我已经想好了一个1到100之间的数字。”);
function promptForGuess() {
readline.question(‘请猜一个数字:’, (input) => {
guess = parseInt(input, 10);
attempts++;
if (isNaN(guess)) {
console.log("请输入一个有效的数字!");
promptForGuess(); // Recursive call for invalid input
} else if (guess < numberToGuess) {
console.log("太小了!");
promptForGuess();
} else if (guess > numberToGuess) {
console.log("太大了!");
promptForGuess();
} else {
console.log(`恭喜你!答案就是${numberToGuess}。`);
console.log(`你总共猜了${attempts}次。`);
readline.close();
}
});
}
promptForGuess();
}
guessNumberGame();
以上是用三种不同编程语言实现的简单猜数字游戏的基本框架。每种语言都有其独特的语法和库来处理输入输出、随机数生成