Python 版本
python
import random
def game(): hezhongliancai.com
number_to_guess = random.randint(1, 100)
guess = None
num_guesses = 0
while guess != number_to_guess:
guess = int(input('猜一个1到100之间的数字: '))
num_guesses += 1
if guess < number_to_guess:
print('太小了!')
elif guess > number_to_guess:
print('太大了!')
print(f'恭喜你,你猜对了!你用了{num_guesses}次猜测。')
if name == “main”:
game()
JavaScript (Node.js) 版本
javascript
const readline = require(‘readline’);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const numberToGuess = Math.floor(Math.random() * 100) + 1;
let numGuesses = 0;
function game() {
rl.question('猜一个1到100之间的数字: ', (guess) => {
numGuesses++;
guess = parseInt(guess, 10);
if (guess < numberToGuess) {
console.log(‘太小了!’);
game();
} else if (guess > numberToGuess) {
console.log(‘太大了!’);
game();
} else {
console.log(恭喜你,你猜对了!你用了${numGuesses}次猜测。
);
rl.close();
}
});
}
game();
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;
int numGuesses = 0;
Scanner scanner = new Scanner(System.in);
do {
System.out.print("猜一个1到100之间的数字: ");
guess = scanner.nextInt();
numGuesses++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
} while (guess != numberToGuess);
System.out.println("恭喜你,你猜对了!你用了" + numGuesses + "次猜测。");
scanner.close();
}
}
这些代码示例分别用Python、JavaScript (Node.js) 和 Java 实现了一个简单的猜数字游戏。游戏会生成一个1到100之间的随机数,然后让用户不断猜测,直到猜中为止。当然可以。以下是用几种不同的编程语言生成的一个简单的猜数字游戏。
Python 版本
python
import random
def game():
number_to_guess = random.randint(1, 100)
guess = None
num_guesses = 0
while guess != number_to_guess:
guess = int(input('猜一个1到100之间的数字: '))
num_guesses += 1
if guess < number_to_guess:
print('太小了!')
elif guess > number_to_guess:
print('太大了!')
print(f'恭喜你,你猜对了!你用了{num_guesses}次猜测。')
if name == “main”:
game()
JavaScript (Node.js) 版本
javascript
const readline = require(‘readline’);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const numberToGuess = Math.floor(Math.random() * 100) + 1;
let numGuesses = 0;
function game() {
rl.question('猜一个1到100之间的数字: ', (guess) => {
numGuesses++;
guess = parseInt(guess, 10);
if (guess < numberToGuess) {
console.log(‘太小了!’);
game();
} else if (guess > numberToGuess) {
console.log(‘太大了!’);
game();
} else {
console.log(恭喜你,你猜对了!你用了${numGuesses}次猜测。
);
rl.close();
}
});
}
game();
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;
int numGuesses = 0;
Scanner scanner = new Scanner(System.in);
do {
System.out.print("猜一个1到100之间的数字: ");
guess = scanner.nextInt();
numGuesses++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
} while (guess != numberToGuess);
System.out.println("恭喜你,你猜对了!你用了" + numGuesses + "次猜测。");
scanner.close();
}
}
这些代码示例分别用Python、JavaScript (Node.js) 和 Java 实现了一个简单的猜数字游戏。游戏会生成一个1到100之间的随机数,然后让用户不断猜测,直到猜中为止。