Python
python
import random
def guess_number():
number_to_guess = random.randint(1, 100) wandarealmwuhan.cn
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'恭喜你,猜对了!你用了{attempts}次尝试。')
guess_number()
JavaScript (Node.js)
javascript
const readline = require(‘readline’);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function guessNumber() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
rl.question('猜一个1到100之间的数字: ', (answer) => {
guess = parseInt(answer);
while (guess !== numberToGuess) {
attempts++;
if (guess < numberToGuess) {
console.log('太小了!');
} else if (guess > numberToGuess) {
console.log('太大了!');
}
rl.question('再试一次: ', (answer) => {
guess = parseInt(answer);
});
}
console.log(`恭喜你,猜对了!你用了${attempts}次尝试。`);
rl.close();
});
}
guessNumber();
Java
Java 的命令行输入/输出更为复杂,因此以下代码只是一个框架,您可能需要使用像 Scanner 这样的类来从用户那里获取输入。
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random rand = new Random();
int numberToGuess = rand.nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
int guess = 0;
int attempts = 0;
while (guess != numberToGuess) {
System.out.print("猜一个1到100之间的数字: ");
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
}
System.out.println("恭喜你,猜对了!你用了" + attempts + "次尝试。");
scanner.close();
}
}
这些示例都是非常基础的,但它们展示了如何在不同的编程语言中创建一个简单的命令行游戏。如果您想创建一个更复杂的游戏,您可能需要学习如何使用图形用户界面(GUI)库、游戏引擎或网络编程来创建多人游戏。由于生成完整的游戏代码对于所有电脑语言来说是一个庞大且复杂的任务,我将为您提供几个简单的示例,这些示例使用不同的编程语言来创建一个基本的命令行游戏——猜数字游戏。
Python
python
import random
def guess_number():
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'恭喜你,猜对了!你用了{attempts}次尝试。')
guess_number()
JavaScript (Node.js)
javascript
const readline = require(‘readline’);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function guessNumber() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
rl.question('猜一个1到100之间的数字: ', (answer) => {
guess = parseInt(answer);
while (guess !== numberToGuess) {
attempts++;
if (guess < numberToGuess) {
console.log('太小了!');
} else if (guess > numberToGuess) {
console.log('太大了!');
}
rl.question('再试一次: ', (answer) => {
guess = parseInt(answer);
});
}
console.log(`恭喜你,猜对了!你用了${attempts}次尝试。`);
rl.close();
});
}
guessNumber();
Java
Java 的命令行输入/输出更为复杂,因此以下代码只是一个框架,您可能需要使用像 Scanner 这样的类来从用户那里获取输入。
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random rand = new Random();
int numberToGuess = rand.nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
int guess = 0;
int attempts = 0;
while (guess != numberToGuess) {
System.out.print("猜一个1到100之间的数字: ");
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
}
System.out.println("恭喜你,猜对了!你用了" + attempts + "次尝试。");
scanner.close();
}
}
这些示例都是非常基础的,但它们展示了如何在不同的编程语言中创建一个简单的命令行游戏。如果您想创建一个更复杂的游戏,您可能需要学习如何使用图形用户界面(GUI)库、游戏引擎或网络编程来创建多人游戏。