- Python 版本
python
import random
def game():
number_to_guess = random.randint(1, 100) kykzl.com
guess = None
attempts = 0
while guess != number_to_guess:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < number_to_guess:
print('太低了!')
elif guess > number_to_guess:
print('太高了!')
print(f'恭喜你,你猜对了!数字是 {number_to_guess},你尝试了 {attempts} 次。')
if name == “main”:
game()
2. JavaScript 版本(在Node.js环境中运行)
javascript
const readline = require(‘readline’);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function game() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
while (guess !== numberToGuess) {
rl.question('猜一个1到100之间的数字: ', (answer) => {
guess = parseInt(answer);
attempts++;
if (guess < numberToGuess) {
console.log('太低了!');
} else if (guess > numberToGuess) {
console.log('太高了!');
} else {
console.log(`恭喜你,你猜对了!数字是 ${numberToGuess},你尝试了 ${attempts} 次。`);
rl.close();
}
if (guess !== numberToGuess) {
game(); // 递归调用以继续循环,但这不是最佳实践
}
});
}
}
game();
注意:JavaScript 的这个示例使用了递归调用,这在实际应用中可能不是最佳实践,因为它会导致堆栈增长。但在控制台应用中,由于递归深度通常很浅,这通常不是问题。
- C++ 版本
cpp
#include
#include
#include
int main() {
std::srand(std::time(0)); // 初始化随机数生成器
int numberToGuess = std::rand() % 100 + 1;
int guess;
int attempts = 0;
do {
std::cout << "猜一个1到100之间的数字: ";
std::cin >> guess;
attempts++;
if (guess < numberToGuess) {
std::cout << "太低了!" << std::endl;
} else if (guess > numberToGuess) {
std::cout << "太高了!" << std::endl;
}
} while (guess != numberToGuess);
std::cout << "恭喜你,你猜对了!数字是 " << numberToGuess << ",你尝试了 " << attempts << " 次。" << std::endl;
return 0;
}
由于不同编程语言的特点和用途各异,我将为你提供三种常见编程语言(Python、JavaScript、C++)的简单控制台游戏代码示例:一个基本的猜数字游戏。
- Python 版本
python
import random
def game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < number_to_guess:
print('太低了!')
elif guess > number_to_guess:
print('太高了!')
print(f'恭喜你,你猜对了!数字是 {number_to_guess},你尝试了 {attempts} 次。')
if name == “main”:
game()
2. JavaScript 版本(在Node.js环境中运行)
javascript
const readline = require(‘readline’);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function game() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
while (guess !== numberToGuess) {
rl.question('猜一个1到100之间的数字: ', (answer) => {
guess = parseInt(answer);
attempts++;
if (guess < numberToGuess) {
console.log('太低了!');
} else if (guess > numberToGuess) {
console.log('太高了!');
} else {
console.log(`恭喜你,你猜对了!数字是 ${numberToGuess},你尝试了 ${attempts} 次。`);
rl.close();
}
if (guess !== numberToGuess) {
game(); // 递归调用以继续循环,但这不是最佳实践
}
});
}
}
game();
注意:JavaScript 的这个示例使用了递归调用,这在实际应用中可能不是最佳实践,因为它会导致堆栈增长。但在控制台应用中,由于递归深度通常很浅,这通常不是问题。
- C++ 版本
cpp
#include
#include
#include
int main() {
std::srand(std::time(0)); // 初始化随机数生成器
int numberToGuess = std::rand() % 100 + 1;
int guess;
int attempts = 0;
do {
std::cout << "猜一个1到100之间的数字: ";
std::cin >> guess;
attempts++;
if (guess < numberToGuess) {
std::cout << "太低了!" << std::endl;
} else if (guess > numberToGuess) {
std::cout << "太高了!" << std::endl;
}
} while (guess != numberToGuess);
std::cout << "恭喜你,你猜对了!数字是 " << numberToGuess << ",你尝试了 " << attempts << " 次。" << std::endl;
return 0;
}