- Python 版本etym.cn
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("我已经想好了一个1到100之间的数字。你能猜到是哪个吗?")
while guess != number_to_guess:
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}次。")
except ValueError:
print("请输入一个有效的数字。")
if name == “main”:
guess_number_game()
2. JavaScript 版本(Node.js 环境)
javascript
const readline = require(‘readline’).createInterface({
input: process.stdin,
output: process.stdout
});
const numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function guessNumberGame() {
console.log(“我已经想好了一个1到100之间的数字。你能猜到是哪个吗?”);
function promptForGuess() {
readline.question("请输入你的猜测: ", (input) => {
if (!isNaN(input) && Number.isInteger(+input)) {
guess = parseInt(input, 10);
attempts++;
if (guess < numberToGuess) {
console.log("太低了!");
} else if (guess > numberToGuess) {
console.log("太高了!");
} else {
console.log(`恭喜你!猜对了!数字就是${numberToGuess}。你尝试了${attempts}次。`);
readline.close();
}
promptForGuess(); // 继续询问直到猜对
} else {
console.log("请输入一个有效的数字。");
promptForGuess(); // 无效输入时重新询问
}
});
}
promptForGuess(); // 开始询问
}
guessNumberGame();
3. Java 版本
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random random = new Random();
int numberToGuess = random.nextInt(100) + 1;
int guess = 0;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("我已经想好了一个1到100之间的数字。你能猜到是哪个吗?");
while (guess != numberToGuess) {
System.out.print("请输入你的猜测: ");
if (scanner.hasNextInt()) {
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太低了!");
} else if (guess > numberToGuess) {
System.out.println("太高了!");
} else {
System.out.println("恭喜你!猜对了!数字就是" + numberToGuess + "。你尝试了" + attempts + "次。");
}
} else {
System.out.println("请输入一个有效的数字。");
scanner.next(); // 消耗掉非整数输入
}
}
scanner.close();
}
}
这些示例展示了如何使用不同的编程语言来实现一个简单的猜数字游戏。每个游戏都包含了一个循环,直到玩家猜中数字为止,同时给出了适当的提示来帮助玩家进行猜测。当然,我可以继续为你提供不同编程语言中实现的简单小游戏代码示例。这次,我们将实现一个猜数字的小游戏,其中计算机随机选择一个数字,玩家尝试猜测这个数字,直到猜对为止。
- Python 版本
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("我已经想好了一个1到100之间的数字。你能猜到是哪个吗?")
while guess != number_to_guess:
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}次。")
except ValueError:
print("请输入一个有效的数字。")
if name == “main”:
guess_number_game()
2. JavaScript 版本(Node.js 环境)
javascript
const readline = require(‘readline’).createInterface({
input: process.stdin,
output: process.stdout
});
const numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function guessNumberGame() {
console.log(“我已经想好了一个1到100之间的数字。你能猜到是哪个吗?”);
function promptForGuess() {
readline.question("请输入你的猜测: ", (input) => {
if (!isNaN(input) && Number.isInteger(+input)) {
guess = parseInt(input, 10);
attempts++;
if (guess < numberToGuess) {
console.log("太低了!");
} else if (guess > numberToGuess) {
console.log("太高了!");
} else {
console.log(`恭喜你!猜对了!数字就是${numberToGuess}。你尝试了${attempts}次。`);
readline.close();
}
promptForGuess(); // 继续询问直到猜对
} else {
console.log("请输入一个有效的数字。");
promptForGuess(); // 无效输入时重新询问
}
});
}
promptForGuess(); // 开始询问
}
guessNumberGame();
3. Java 版本
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random random = new Random();
int numberToGuess = random.nextInt(100) + 1;
int guess = 0;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("我已经想好了一个1到100之间的数字。你能猜到是哪个吗?");
while (guess != numberToGuess) {
System.out.print("请输入你的猜测: ");
if (scanner.hasNextInt()) {
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太低了!");
} else if (guess > numberToGuess) {
System.out.println("太高了!");
} else {
System.out.println("恭喜你!猜对了!数字就是" + numberToGuess + "。你尝试了" + attempts + "次。");
}
} else {
System.out.println("请输入一个有效的数字。");
scanner.next(); // 消耗掉非整数输入
}
}
scanner.close();
}
}
这些示例展示了如何使用不同的编程语言来实现一个简单的猜数字游戏。每个游戏都包含了一个循环,直到玩家猜中数字为止,同时给出了适当的提示来帮助玩家进行猜测。