- Python - 猜数字游戏
python
import random
def guess_number(): cnavmall.cn
secret_number = random.randint(1, 100)
guess = None
attempts = 0
while guess != secret_number:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < secret_number:
print('猜的数字太小了')
elif guess > secret_number:
print('猜的数字太大了')
print(f'恭喜你,猜对了!数字是 {secret_number}。你尝试了 {attempts} 次。')
if name == “main”:
guess_number()
2. JavaScript (Node.js环境) - 猜数字游戏
javascript
const readline = require(‘readline’);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let secretNumber = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function guessNumber() {
rl.question('猜一个1到100之间的数字: ', (input) => {
guess = parseInt(input);
attempts++;
if (guess < secretNumber) {
console.log(‘猜的数字太小了’);
guessNumber();
} else if (guess > secretNumber) {
console.log(‘猜的数字太大了’);
guessNumber();
} else {
console.log(恭喜你,猜对了!数字是 ${secretNumber}。你尝试了 ${attempts} 次。
);
rl.close();
}
});
}
guessNumber();
3. C++ - 猜数字游戏
cpp
#include
#include
#include
int main() {
std::srand(std::time(0)); // 初始化随机数种子
int secretNumber = std::rand() % 100 + 1;
int guess = 0;
int attempts = 0;
do {
std::cout << "猜一个1到100之间的数字: ";
std::cin >> guess;
attempts++;
if (guess < secretNumber) {
std::cout << "猜的数字太小了" << std::endl;
} else if (guess > secretNumber) {
std::cout << "猜的数字太大了" << std::endl;
}
} while (guess != secretNumber);
std::cout << "恭喜你,猜对了!数字是 " << secretNumber << "。你尝试了 " << attempts << " 次。" << std::endl;
return 0;
}
以上三个示例都是基于“猜数字”的简单游戏,你可以根据自己的需求进行修改和扩展。由于不同编程语言的特点和用途各不相同,以下我将为你提供三种常见编程语言(Python、JavaScript、C++)的简单小游戏代码示例。这些示例都将是基于控制台或终端的简单游戏。
- Python - 猜数字游戏
python
import random
def guess_number():
secret_number = random.randint(1, 100)
guess = None
attempts = 0
while guess != secret_number:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < secret_number:
print('猜的数字太小了')
elif guess > secret_number:
print('猜的数字太大了')
print(f'恭喜你,猜对了!数字是 {secret_number}。你尝试了 {attempts} 次。')
if name == “main”:
guess_number()
2. JavaScript (Node.js环境) - 猜数字游戏
javascript
const readline = require(‘readline’);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let secretNumber = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function guessNumber() {
rl.question('猜一个1到100之间的数字: ', (input) => {
guess = parseInt(input);
attempts++;
if (guess < secretNumber) {
console.log(‘猜的数字太小了’);
guessNumber();
} else if (guess > secretNumber) {
console.log(‘猜的数字太大了’);
guessNumber();
} else {
console.log(恭喜你,猜对了!数字是 ${secretNumber}。你尝试了 ${attempts} 次。
);
rl.close();
}
});
}
guessNumber();
3. C++ - 猜数字游戏
cpp
#include
#include
#include
int main() {
std::srand(std::time(0)); // 初始化随机数种子
int secretNumber = std::rand() % 100 + 1;
int guess = 0;
int attempts = 0;
do {
std::cout << "猜一个1到100之间的数字: ";
std::cin >> guess;
attempts++;
if (guess < secretNumber) {
std::cout << "猜的数字太小了" << std::endl;
} else if (guess > secretNumber) {
std::cout << "猜的数字太大了" << std::endl;
}
} while (guess != secretNumber);
std::cout << "恭喜你,猜对了!数字是 " << secretNumber << "。你尝试了 " << attempts << " 次。" << std::endl;
return 0;
}
以上三个示例都是基于“猜数字”的简单游戏,你可以根据自己的需求进行修改和扩展。