Python 版本
python
import random
def game():
number_to_guess = random.randint(1, 100) wiee.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} 次尝试。")
if name == “main”:
game()
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(`恭喜你,猜对了!你用了 ${attempts} 次尝试。`);
rl.close();
}
if (guess !== numberToGuess) {
game(); // 递归调用以继续游戏循环
}
});
}
}
game();
注意:由于 JavaScript 的 readline 模块是异步的,上面的代码使用了递归调用以保持游戏循环。在真实应用中,你可能希望使用更优雅的解决方案,如 Promise 或 async/await。
Java 版本
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.printf("恭喜你,猜对了!你用了 %d 次尝试。%n", attempts);
scanner.close();
}
}
由于不同编程语言的特点和用途各异,这里我将为你提供三种常见编程语言(Python、JavaScript、Java)的简单控制台游戏代码示例:猜数字游戏。
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"恭喜你,猜对了!你用了 {attempts} 次尝试。")
if name == “main”:
game()
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(`恭喜你,猜对了!你用了 ${attempts} 次尝试。`);
rl.close();
}
if (guess !== numberToGuess) {
game(); // 递归调用以继续游戏循环
}
});
}
}
game();
注意:由于 JavaScript 的 readline 模块是异步的,上面的代码使用了递归调用以保持游戏循环。在真实应用中,你可能希望使用更优雅的解决方案,如 Promise 或 async/await。
Java 版本
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.printf("恭喜你,猜对了!你用了 %d 次尝试。%n", attempts);
scanner.close();
}
}