Python 示例
python
import random
number_to_guess = random.randint(1, 100) hellohailuo.com
guess = None
attempts = 0
print(“欢迎来到猜数字游戏!”)
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}。”)
print(f"你总共尝试了{attempts}次。”)
except ValueError:
print(“请输入一个有效的数字!”)
JavaScript (HTML + JS) 示例
HTML 部分:
html
猜数字游戏
我已经想好了一个1到100之间的数字。你能猜到是哪个吗?
猜一下<script src="game.js"></script>
JavaScript 部分 (game.js):
javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function checkGuess() {
guess = parseInt(document.getElementById(‘guessInput’).value);
attempts++;
const feedback = document.getElementById(‘feedback’);
if (isNaN(guess)) {
feedback.textContent = "请输入一个有效的数字!";
} else if (guess < numberToGuess) {
feedback.textContent = "太小了!";
} else if (guess > numberToGuess) {
feedback.textContent = "太大了!";
} else {
feedback.textContent = `恭喜你!答案就是${numberToGuess}。你总共尝试了${attempts}次。`;
}
}
Java 示例 (控制台应用)
注意:Java 的控制台应用通常更复杂,因为它需要更多的设置代码。这里仅提供核心逻辑部分。
java
import java.util.Random;
import java.util.Scanner;
public class GuessTheNumber {
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("欢迎来到猜数字游戏!");
System.out.println("我已经想好了一个1到100之间的数字。");
while (guess != numberToGuess) {
System.out.print("请猜一个数字: ");
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
} else {
System.out.println("恭喜你!答案就是" + numberToGuess + "。");
System.out.println("你总共尝试了" + attempts + "次。");
}
}
scanner.close();
}
}
每种语言都有其特定的语法和库,但基本的逻辑是相似的:随机生成一个数字,让玩家猜测,并根据玩家的猜测给出提示,直到猜中为止。由于篇幅限制,我将为你提供几种不同编程语言中简单的“猜数字”游戏示例。这个游戏会随机生成一个1到100之间的数字,玩家需要猜测这个数字是多少,直到猜中为止。
Python 示例
python
import random
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print(“欢迎来到猜数字游戏!”)
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}。”)
print(f"你总共尝试了{attempts}次。”)
except ValueError:
print(“请输入一个有效的数字!”)
JavaScript (HTML + JS) 示例
HTML 部分:
html
猜数字游戏
我已经想好了一个1到100之间的数字。你能猜到是哪个吗?
猜一下<script src="game.js"></script>
JavaScript 部分 (game.js):
javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function checkGuess() {
guess = parseInt(document.getElementById(‘guessInput’).value);
attempts++;
const feedback = document.getElementById(‘feedback’);
if (isNaN(guess)) {
feedback.textContent = "请输入一个有效的数字!";
} else if (guess < numberToGuess) {
feedback.textContent = "太小了!";
} else if (guess > numberToGuess) {
feedback.textContent = "太大了!";
} else {
feedback.textContent = `恭喜你!答案就是${numberToGuess}。你总共尝试了${attempts}次。`;
}
}
Java 示例 (控制台应用)
注意:Java 的控制台应用通常更复杂,因为它需要更多的设置代码。这里仅提供核心逻辑部分。
java
import java.util.Random;
import java.util.Scanner;
public class GuessTheNumber {
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("欢迎来到猜数字游戏!");
System.out.println("我已经想好了一个1到100之间的数字。");
while (guess != numberToGuess) {
System.out.print("请猜一个数字: ");
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
} else {
System.out.println("恭喜你!答案就是" + numberToGuess + "。");
System.out.println("你总共尝试了" + attempts + "次。");
}
}
scanner.close();
}
}
每种语言都有其特定的语法和库,但基本的逻辑是相似的:随机生成一个数字,让玩家猜测,并根据玩家的猜测给出提示,直到猜中为止。