- Python 示例
python
import random
number_to_guess = random.randint(1, 100)
attempts = 0 haoqian168.com
print(“猜猜看,我想的是哪个1到100之间的数字?”)
while True:
try:
guess = int(input("请输入你的猜测: “))
attempts += 1
if guess < number_to_guess:
print(“太低了!”)
elif guess > number_to_guess:
print(“太高了!”)
else:
print(f"恭喜你!答案就是{number_to_guess}。你尝试了{attempts}次。”)
break
except ValueError:
print(“请输入一个有效的数字。”)
2. JavaScript (Node.js 环境) 示例
这个示例需要在Node.js环境中运行,因为它使用了readline模块来处理命令行输入。
javascript
const readline = require(‘readline’).createInterface({
input: process.stdin,
output: process.stdout
});
const numberToGuess = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
console.log(“猜猜看,我想的是哪个1到100之间的数字?”);
readline.prompt("请输入你的猜测: ", function(guess) {
guess = parseInt(guess, 10);
attempts++;
if (isNaN(guess)) {
console.log(“请输入一个有效的数字。”);
readline.prompt("请输入你的猜测: ", arguments.callee);
} else if (guess < numberToGuess) {
console.log(“太低了!”);
readline.prompt("请输入你的猜测: ", arguments.callee);
} else if (guess > numberToGuess) {
console.log(“太高了!”);
readline.prompt("请输入你的猜测: ", arguments.callee);
} else {
console.log(恭喜你!答案就是${numberToGuess}。你尝试了${attempts}次。
);
readline.close();
}
});
3. 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 attempts = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("猜猜看,我想的是哪个1到100之间的数字?");
while (true) {
System.out.print("请输入你的猜测: ");
int guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太低了!");
} else if (guess > numberToGuess) {
System.out.println("太高了!");
} else {
System.out.printf("恭喜你!答案就是%d。你尝试了%d次。\n", numberToGuess, attempts);
break;
}
}
scanner.close();
}
}
这些示例覆盖了Python、JavaScript(Node.js环境)和Java三种不同的编程语言,展示了如何实现一个简单的猜数字游戏。由于篇幅和复杂度的限制,我将为你提供一个简单的猜数字游戏(Guess the Number Game)的示例,使用几种不同的编程语言来实现。这个游戏的基本规则是:程序随机选择一个1到100之间的数字,用户尝试猜测这个数字,程序会告诉用户他们的猜测是太高、太低还是正确。
- Python 示例
python
import random
number_to_guess = random.randint(1, 100)
attempts = 0
print(“猜猜看,我想的是哪个1到100之间的数字?”)
while True:
try:
guess = int(input("请输入你的猜测: “))
attempts += 1
if guess < number_to_guess:
print(“太低了!”)
elif guess > number_to_guess:
print(“太高了!”)
else:
print(f"恭喜你!答案就是{number_to_guess}。你尝试了{attempts}次。”)
break
except ValueError:
print(“请输入一个有效的数字。”)
2. JavaScript (Node.js 环境) 示例
这个示例需要在Node.js环境中运行,因为它使用了readline模块来处理命令行输入。
javascript
const readline = require(‘readline’).createInterface({
input: process.stdin,
output: process.stdout
});
const numberToGuess = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
console.log(“猜猜看,我想的是哪个1到100之间的数字?”);
readline.prompt("请输入你的猜测: ", function(guess) {
guess = parseInt(guess, 10);
attempts++;
if (isNaN(guess)) {
console.log(“请输入一个有效的数字。”);
readline.prompt("请输入你的猜测: ", arguments.callee);
} else if (guess < numberToGuess) {
console.log(“太低了!”);
readline.prompt("请输入你的猜测: ", arguments.callee);
} else if (guess > numberToGuess) {
console.log(“太高了!”);
readline.prompt("请输入你的猜测: ", arguments.callee);
} else {
console.log(恭喜你!答案就是${numberToGuess}。你尝试了${attempts}次。
);
readline.close();
}
});
3. 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 attempts = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("猜猜看,我想的是哪个1到100之间的数字?");
while (true) {
System.out.print("请输入你的猜测: ");
int guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太低了!");
} else if (guess > numberToGuess) {
System.out.println("太高了!");
} else {
System.out.printf("恭喜你!答案就是%d。你尝试了%d次。\n", numberToGuess, attempts);
break;
}
}
scanner.close();
}
}
这些示例覆盖了Python、JavaScript(Node.js环境)和Java三种不同的编程语言,展示了如何实现一个简单的猜数字游戏。