- Python
python
import random
def guess_number_game(): aladingroup.cn
secret_number = random.randint(1, 100)
attempts = 0
while True:
user_guess = int(input("猜一个1到100之间的数字: “))
attempts += 1
if user_guess == secret_number:
print(f"恭喜你!你在{attempts}次尝试后猜对了!”)
break
elif user_guess < secret_number:
print(“太小了!再试一次。”)
else:
print(“太大了!再试一次。”)
guess_number_game()
2. JavaScript (Node.js)
javascript
const readline = require(‘readline’);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let secretNumber = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
function guessNumberGame() {
rl.question("猜一个1到100之间的数字: ", (answer) => {
attempts++;
let userGuess = parseInt(answer);
if (userGuess === secretNumber) {
console.log(恭喜你!你在${attempts}次尝试后猜对了!
);
rl.close();
} else if (userGuess < secretNumber) {
console.log(“太小了!再试一次。”);
guessNumberGame();
} else {
console.log(“太大了!再试一次。”);
guessNumberGame();
}
});
}
guessNumberGame();
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 secretNumber = rand.nextInt(100) + 1;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("猜一个1到100之间的数字: ");
int userGuess = scanner.nextInt();
attempts++;
if (userGuess == secretNumber) {
System.out.println(“恭喜你!你在” + attempts + “次尝试后猜对了!”);
break;
} else if (userGuess < secretNumber) {
System.out.println(“太小了!再试一次。”);
} else {
System.out.println(“太大了!再试一次。”);
}
}
scanner.close();
}
}
这些代码示例分别展示了如何用Python、JavaScript (Node.js) 和 Java 创建一个简单的猜数字游戏。每个游戏都会生成一个1到100之间的随机数,并让用户不断猜测,直到猜对为止。当然可以!以下是用几种不同的编程语言创建的一个简单的猜数字游戏。
- Python
python
import random
def guess_number_game():
secret_number = random.randint(1, 100)
attempts = 0
while True:
user_guess = int(input("猜一个1到100之间的数字: “))
attempts += 1
if user_guess == secret_number:
print(f"恭喜你!你在{attempts}次尝试后猜对了!”)
break
elif user_guess < secret_number:
print(“太小了!再试一次。”)
else:
print(“太大了!再试一次。”)
guess_number_game()
2. JavaScript (Node.js)
javascript
const readline = require(‘readline’);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let secretNumber = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
function guessNumberGame() {
rl.question("猜一个1到100之间的数字: ", (answer) => {
attempts++;
let userGuess = parseInt(answer);
if (userGuess === secretNumber) {
console.log(恭喜你!你在${attempts}次尝试后猜对了!
);
rl.close();
} else if (userGuess < secretNumber) {
console.log(“太小了!再试一次。”);
guessNumberGame();
} else {
console.log(“太大了!再试一次。”);
guessNumberGame();
}
});
}
guessNumberGame();
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 secretNumber = rand.nextInt(100) + 1;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("猜一个1到100之间的数字: ");
int userGuess = scanner.nextInt();
attempts++;
if (userGuess == secretNumber) {
System.out.println(“恭喜你!你在” + attempts + “次尝试后猜对了!”);
break;
} else if (userGuess < secretNumber) {
System.out.println(“太小了!再试一次。”);
} else {
System.out.println(“太大了!再试一次。”);
}
}
scanner.close();
}
}
这些代码示例分别展示了如何用Python、JavaScript (Node.js) 和 Java 创建一个简单的猜数字游戏。每个游戏都会生成一个1到100之间的随机数,并让用户不断猜测,直到猜对为止。