
Python:
python
import random
def guess_number(): mdthv.cn
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < number_to_guess:
print('太低了!')
elif guess > number_to_guess:
print('太高了!')
print(f'恭喜你,猜对了!用了{attempts}次尝试。')
guess_number()
JavaScript (Node.js):
javascript
const readline = require(‘readline’).createInterface({
input: process.stdin,
output: process.stdout
});
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function promptGuess() {
readline.question('猜一个1到100之间的数字: ', (input) => {
guess = parseInt(input);
attempts++;
if (guess < numberToGuess) {
console.log(‘太低了!’);
} else if (guess > numberToGuess) {
console.log(‘太高了!’);
} else {
console.log(恭喜你,猜对了!用了${attempts}次尝试。);
readline.close();
}
promptGuess();
});
}
promptGuess();
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;
Scanner scanner = new Scanner(System.in);
int guess = 0;
int attempts = 0;
while (guess != numberToGuess) {
System.out.print("猜一个1到100之间的数字: ");
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太低了!");
} else if (guess > numberToGuess) {
System.out.println("太高了!");
}
}
System.out.println("恭喜你,猜对了!用了" + attempts + "次尝试。");
scanner.close();
}
}
这些代码示例都是基于控制台的简单猜数字游戏。每个程序都会随机生成一个1到100之间的数字,然后让用户尝试猜测它。程序会告诉用户他们的猜测是太高、太低还是正确,并记录尝试次数。由于篇幅限制,我将为您提供几种不同编程语言中的简单游戏代码示例:一个基于控制台的猜数字游戏。
Python:
python
import random
def guess_number():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < number_to_guess:
print('太低了!')
elif guess > number_to_guess:
print('太高了!')
print(f'恭喜你,猜对了!用了{attempts}次尝试。')
guess_number()
JavaScript (Node.js):
javascript
const readline = require(‘readline’).createInterface({
input: process.stdin,
output: process.stdout
});
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function promptGuess() {
readline.question('猜一个1到100之间的数字: ', (input) => {
guess = parseInt(input);
attempts++;
if (guess < numberToGuess) {
console.log(‘太低了!’);
} else if (guess > numberToGuess) {
console.log(‘太高了!’);
} else {
console.log(恭喜你,猜对了!用了${attempts}次尝试。);
readline.close();
}
promptGuess();
});
}
promptGuess();
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;
Scanner scanner = new Scanner(System.in);
int guess = 0;
int attempts = 0;
while (guess != numberToGuess) {
System.out.print("猜一个1到100之间的数字: ");
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太低了!");
} else if (guess > numberToGuess) {
System.out.println("太高了!");
}
}
System.out.println("恭喜你,猜对了!用了" + attempts + "次尝试。");
scanner.close();
}
}
这些代码示例都是基于控制台的简单猜数字游戏。每个程序都会随机生成一个1到100之间的数字,然后让用户尝试猜测它。程序会告诉用户他们的猜测是太高、太低还是正确,并记录尝试次数。

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



