
- Python猜数字游戏
python
import random
def guess_number(): wandarealmwuhan.cn
number = random.randint(1, 100)
attempts = 0
while True:
user_guess = int(input("猜一个1到100之间的数字: "))
attempts += 1
if user_guess == number:
print(f"恭喜你,猜对了!你用了{attempts}次尝试。")
break
elif user_guess < number:
print("太小了!再试一次。")
else:
print("太大了!再试一次。")
guess_number()
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;
function askForGuess() {
rl.question("猜一个1到100之间的数字: ", (guess) => {
attempts++;
const userGuess = parseInt(guess, 10);
if (userGuess === number) {
console.log(`恭喜你,猜对了!你用了${attempts}次尝试。`);
rl.close();
} else if (userGuess < number) {
console.log("太小了!再试一次。");
askForGuess();
} else {
console.log("太大了!再试一次。");
askForGuess();
}
});
}
askForGuess();
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);
while (true) {
System.out.println("猜一个1到100之间的数字: ");
int guess = scanner.nextInt();
attempts++;
if (guess == number) {
System.out.println("恭喜你,猜对了!你用了" + attempts + "次尝试。");
break;
} else if (guess < number) {
System.out.println("太小了!再试一次。");
} else {
System.out.println("太大了!再试一次。");
}
}
scanner.close();
}
}
以上三个示例都是简单的猜数字游戏,程序会随机生成一个1到100之间的数字,然后让用户来猜。根据用户的猜测,程序会给出相应的提示,直到用户猜对为止。当然可以。下面我会为你提供三个简单的小游戏示例,分别使用Python、JavaScript(Node.js环境)和Java。
- Python猜数字游戏
python
import random
def guess_number():
number = random.randint(1, 100)
attempts = 0
while True:
user_guess = int(input("猜一个1到100之间的数字: "))
attempts += 1
if user_guess == number:
print(f"恭喜你,猜对了!你用了{attempts}次尝试。")
break
elif user_guess < number:
print("太小了!再试一次。")
else:
print("太大了!再试一次。")
guess_number()
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;
function askForGuess() {
rl.question("猜一个1到100之间的数字: ", (guess) => {
attempts++;
const userGuess = parseInt(guess, 10);
if (userGuess === number) {
console.log(`恭喜你,猜对了!你用了${attempts}次尝试。`);
rl.close();
} else if (userGuess < number) {
console.log("太小了!再试一次。");
askForGuess();
} else {
console.log("太大了!再试一次。");
askForGuess();
}
});
}
askForGuess();
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);
while (true) {
System.out.println("猜一个1到100之间的数字: ");
int guess = scanner.nextInt();
attempts++;
if (guess == number) {
System.out.println("恭喜你,猜对了!你用了" + attempts + "次尝试。");
break;
} else if (guess < number) {
System.out.println("太小了!再试一次。");
} else {
System.out.println("太大了!再试一次。");
}
}
scanner.close();
}
}
以上三个示例都是简单的猜数字游戏,程序会随机生成一个1到100之间的数字,然后让用户来猜。根据用户的猜测,程序会给出相应的提示,直到用户猜对为止。

被折叠的 条评论
为什么被折叠?



