- Python 版本
python
import random
def guess_number_game():
number = random.randint(1, 100) rjdxjy.com
attempts = 0
print("欢迎来到猜数字游戏!数字范围是1到100。")
while True:
user_guess = int(input("请输入你猜测的数字:"))
attempts += 1
if user_guess < number:
print("太小了!再试一次。")
elif user_guess > number:
print("太大了!再试一次。")
else:
print(f"恭喜你!你猜对了!你总共尝试了{attempts}次。")
break
guess_number_game()
2. JavaScript (Node.js) 版本
javascript
const readline = require(‘readline’);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const number = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
console.log(“欢迎来到猜数字游戏!数字范围是1到100。”);
rl.setPrompt(‘请输入你猜测的数字:’);
rl.prompt();
rl.on(‘line’, (input) => {
attempts++;
const guess = parseInt(input, 10);
if (guess < number) {
console.log("太小了!再试一次。");
} else if (guess > number) {
console.log("太大了!再试一次。");
} else {
console.log(`恭喜你!你猜对了!你总共尝试了${attempts}次。`);
rl.close();
}
rl.prompt();
});
3. Java 版本
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random rand = new Random();
int number = 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 < number) {
System.out.println("太小了!再试一次。");
} else if (guess > number) {
System.out.println("太大了!再试一次。");
} else {
System.out.println("恭喜你!你猜对了!你总共尝试了" + attempts + "次。");
break;
}
}
scanner.close();
}
}
这些代码分别用Python、JavaScript(Node.js)和Java实现了一个简单的猜数字游戏。你可以根据自己的需要选择运行其中一个版本。当然可以!下面我会用Python、JavaScript(Node.js)和Java三种语言分别为你提供一个简单的猜数字游戏代码。
- Python 版本
python
import random
def guess_number_game():
number = random.randint(1, 100)
attempts = 0
print("欢迎来到猜数字游戏!数字范围是1到100。")
while True:
user_guess = int(input("请输入你猜测的数字:"))
attempts += 1
if user_guess < number:
print("太小了!再试一次。")
elif user_guess > number:
print("太大了!再试一次。")
else:
print(f"恭喜你!你猜对了!你总共尝试了{attempts}次。")
break
guess_number_game()
2. JavaScript (Node.js) 版本
javascript
const readline = require(‘readline’);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const number = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
console.log(“欢迎来到猜数字游戏!数字范围是1到100。”);
rl.setPrompt(‘请输入你猜测的数字:’);
rl.prompt();
rl.on(‘line’, (input) => {
attempts++;
const guess = parseInt(input, 10);
if (guess < number) {
console.log("太小了!再试一次。");
} else if (guess > number) {
console.log("太大了!再试一次。");
} else {
console.log(`恭喜你!你猜对了!你总共尝试了${attempts}次。`);
rl.close();
}
rl.prompt();
});
3. Java 版本
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random rand = new Random();
int number = 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 < number) {
System.out.println("太小了!再试一次。");
} else if (guess > number) {
System.out.println("太大了!再试一次。");
} else {
System.out.println("恭喜你!你猜对了!你总共尝试了" + attempts + "次。");
break;
}
}
scanner.close();
}
}
这些代码分别用Python、JavaScript(Node.js)和Java实现了一个简单的猜数字游戏。你可以根据自己的需要选择运行其中一个版本。