Python 版本
python
import random
def guess_number(): maoxianjiu.cn
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”:
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;
function askForGuess() {
rl.question('猜一个1到100之间的数字: ', (input) => {
guess = parseInt(input);
attempts++;
if (guess < numberToGuess) {
console.log('太小了!');
askForGuess();
} else if (guess > numberToGuess) {
console.log('太大了!');
askForGuess();
} else {
console.log(`恭喜你,猜对了!数字是 ${numberToGuess},你尝试了 ${attempts} 次。`);
rl.close();
}
});
}
askForGuess();
}
guessNumber();
Java 版本
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;
int guess = 0;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
while (guess != numberToGuess) {
System.out.print("猜一个1到100之间的数字: ");
try {
guess = scanner.nextInt();
} catch (Exception e) {
System.out.println("输入无效,请输入一个整数。");
scanner.nextLine(); // 清除错误输入
continue;
}
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
}
System.out.printf("恭喜你,猜对了!数字是 %d,你尝试了 %d 次。\n", numberToGuess, attempts);
scanner.close();
}
}
这些代码示例提供了一个简单的“猜数字”游戏,玩家需要在给定的范围内猜测一个随机生成的数字。每个版本都包含了基本的输入/输出逻辑和循环结构,以便玩家可以持续猜测直到猜对为止。由于生成完整的游戏代码在不同语言中会相对复杂,我将为您提供一个简单的小游戏示例,这个游戏是一个基于控制台(命令行)的“猜数字”游戏。我将用Python、JavaScript(Node.js环境)和Java来编写这个游戏的简化版本。
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"恭喜你,猜对了!数字是 {number_to_guess},你尝试了 {attempts} 次。")
if name == “main”:
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;
function askForGuess() {
rl.question('猜一个1到100之间的数字: ', (input) => {
guess = parseInt(input);
attempts++;
if (guess < numberToGuess) {
console.log('太小了!');
askForGuess();
} else if (guess > numberToGuess) {
console.log('太大了!');
askForGuess();
} else {
console.log(`恭喜你,猜对了!数字是 ${numberToGuess},你尝试了 ${attempts} 次。`);
rl.close();
}
});
}
askForGuess();
}
guessNumber();
Java 版本
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;
int guess = 0;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
while (guess != numberToGuess) {
System.out.print("猜一个1到100之间的数字: ");
try {
guess = scanner.nextInt();
} catch (Exception e) {
System.out.println("输入无效,请输入一个整数。");
scanner.nextLine(); // 清除错误输入
continue;
}
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
}
System.out.printf("恭喜你,猜对了!数字是 %d,你尝试了 %d 次。\n", numberToGuess, attempts);
scanner.close();
}
}
这些代码示例提供了一个简单的“猜数字”游戏,玩家需要在给定的范围内猜测一个随机生成的数字。每个版本都包含了基本的输入/输出逻辑和循环结构,以便玩家可以持续猜测直到猜对为止。