- Python 版本
python
import random
def guess_number_game(): cy2.cn
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("猜数字游戏!我已经想好了一个1到100之间的数字。")
while guess != number_to_guess:
try:
guess = int(input("请输入你的猜测:"))
attempts += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
else:
print(f"恭喜你,猜对了!数字就是{number_to_guess}。你尝试了{attempts}次。")
except ValueError:
print("请输入一个有效的数字。")
if name == “main”:
guess_number_game()
2. JavaScript (HTML + JavaScript) 版本
html
猜数字游戏
我已经想好了一个1到100之间的数字,你能猜到是多少吗?
提交<script>
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function checkGuess() {
guess = parseInt(document.getElementById('guessInput').value, 10);
attempts++;
if (isNaN(guess)) {
document.getElementById('result').innerText = "请输入一个有效的数字。";
return;
}
if (guess < numberToGuess) {
document.getElementById('result').innerText = "太小了!";
} else if (guess > numberToGuess) {
document.getElementById('result').innerText = "太大了!";
} else {
document.getElementById('result').innerText = `恭喜你,猜对了!数字就是${numberToGuess}。你尝试了${attempts}次。`;
}
}
</script>
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 numberToGuess = rand.nextInt(100) + 1;
int guess = 0;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("猜数字游戏!我已经想好了一个1到100之间的数字。");
while (guess != numberToGuess) {
System.out.print("请输入你的猜测:");
while (!scanner.hasNextInt()) {
System.out.println("这不是一个有效的数字!请重新输入。");
scanner.next(); // 清除无效输入
}
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
} else {
System.out.printf("恭喜你,猜对了!数字就是%d。你尝试了%d次。\n", numberToGuess, attempts);
}
}
scanner.close();
}
}
以上三个示例分别展示了如何使用Python、JavaScript(在网页环境中)和Java(在控制台应用程序中)来实现一个简单的猜数字游戏。每个示例都包含了基本的用户输入、随机数生成、条件判断和循环控制等编程概念。当然,为了展示不同编程语言在编写小程序游戏时的差异,我将继续为你提供几种不同语言的“猜数字”游戏示例。这个游戏相对简单,但足以展示基本的输入输出、条件判断和循环控制。
- Python 版本
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("猜数字游戏!我已经想好了一个1到100之间的数字。")
while guess != number_to_guess:
try:
guess = int(input("请输入你的猜测:"))
attempts += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
else:
print(f"恭喜你,猜对了!数字就是{number_to_guess}。你尝试了{attempts}次。")
except ValueError:
print("请输入一个有效的数字。")
if name == “main”:
guess_number_game()
2. JavaScript (HTML + JavaScript) 版本
html
猜数字游戏
我已经想好了一个1到100之间的数字,你能猜到是多少吗?
提交<script>
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function checkGuess() {
guess = parseInt(document.getElementById('guessInput').value, 10);
attempts++;
if (isNaN(guess)) {
document.getElementById('result').innerText = "请输入一个有效的数字。";
return;
}
if (guess < numberToGuess) {
document.getElementById('result').innerText = "太小了!";
} else if (guess > numberToGuess) {
document.getElementById('result').innerText = "太大了!";
} else {
document.getElementById('result').innerText = `恭喜你,猜对了!数字就是${numberToGuess}。你尝试了${attempts}次。`;
}
}
</script>
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 numberToGuess = rand.nextInt(100) + 1;
int guess = 0;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("猜数字游戏!我已经想好了一个1到100之间的数字。");
while (guess != numberToGuess) {
System.out.print("请输入你的猜测:");
while (!scanner.hasNextInt()) {
System.out.println("这不是一个有效的数字!请重新输入。");
scanner.next(); // 清除无效输入
}
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
} else {
System.out.printf("恭喜你,猜对了!数字就是%d。你尝试了%d次。\n", numberToGuess, attempts);
}
}
scanner.close();
}
}
以上三个示例分别展示了如何使用Python、JavaScript(在网页环境中)和Java(在控制台应用程序中)来实现一个简单的猜数字游戏。每个示例都包含了基本的用户输入、随机数生成、条件判断和循环控制等编程概念。