Python
gzrrgx.com
python
import random
def guess_number():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
try:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < number_to_guess:
print('太小了!')
elif guess > number_to_guess:
print('太大了!')
except ValueError:
print('请输入一个整数!')
print(f'恭喜你,你猜对了!用了{attempts}次尝试。')
if name == “main”:
guess_number()
JavaScript (在浏览器控制台或Node.js中运行)
javascript
function guessNumber() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
while (guess !== numberToGuess) {
guess = prompt('猜一个1到100之间的数字:');
attempts++;
if (guess < numberToGuess) {
console.log('太小了!');
} else if (guess > numberToGuess) {
console.log('太大了!');
} else if (isNaN(guess)) {
console.log('请输入一个整数!');
guess = null; // Reset guess if not a number
}
}
console.log(`恭喜你,你猜对了!用了${attempts}次尝试。`);
}
guessNumber();
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 = 0;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
while (guess != numberToGuess) {
System.out.print("猜一个1到100之间的数字: ");
if (scanner.hasNextInt()) {
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
} else {
System.out.println("请输入一个整数!");
scanner.next(); // Consume the non-integer input
guess = 0; // Reset guess if not a number
}
}
System.out.println("恭喜你,你猜对了!用了" + attempts + "次尝试。");
}
}
这些代码示例提供了一个简单的“猜数字”游戏,玩家需要在限定的范围内猜测一个随机生成的数字。每个示例都使用了不同的输入/输出方法,以适应各自编程环境的特性。由于编写完整的小程序游戏代码对于多种编程语言来说可能太长,我将为你提供用三种流行的编程语言(Python、JavaScript 和 Java)编写的简单“猜数字”游戏的基础框架。
Python
python
import random
def guess_number():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
try:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < number_to_guess:
print('太小了!')
elif guess > number_to_guess:
print('太大了!')
except ValueError:
print('请输入一个整数!')
print(f'恭喜你,你猜对了!用了{attempts}次尝试。')
if name == “main”:
guess_number()
JavaScript (在浏览器控制台或Node.js中运行)
javascript
function guessNumber() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
while (guess !== numberToGuess) {
guess = prompt('猜一个1到100之间的数字:');
attempts++;
if (guess < numberToGuess) {
console.log('太小了!');
} else if (guess > numberToGuess) {
console.log('太大了!');
} else if (isNaN(guess)) {
console.log('请输入一个整数!');
guess = null; // Reset guess if not a number
}
}
console.log(`恭喜你,你猜对了!用了${attempts}次尝试。`);
}
guessNumber();
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 = 0;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
while (guess != numberToGuess) {
System.out.print("猜一个1到100之间的数字: ");
if (scanner.hasNextInt()) {
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
} else {
System.out.println("请输入一个整数!");
scanner.next(); // Consume the non-integer input
guess = 0; // Reset guess if not a number
}
}
System.out.println("恭喜你,你猜对了!用了" + attempts + "次尝试。");
}
}
这些代码示例提供了一个简单的“猜数字”游戏,玩家需要在限定的范围内猜测一个随机生成的数字。每个示例都使用了不同的输入/输出方法,以适应各自编程环境的特性。