- Python 版本621112.com
python
import random
def guess_number_game():
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"恭喜你!答对了。你总共尝试了{attempts}次。")
except ValueError:
print("请输入一个有效的数字。")
if name == “main”:
guess_number_game()
2. JavaScript 版本(适用于网页)
HTML 部分(index.html):
html
猜数字游戏
我已经选择了一个1到100之间的数字。请猜这个数字是多少:
猜测 JavaScript 部分(game.js):javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function guess() {
guess = parseInt(document.getElementById(‘guessInput’).value);
attempts++;
if (isNaN(guess)) {
document.getElementById('feedback').innerText = "请输入一个有效的数字。";
return;
}
if (guess < numberToGuess) {
document.getElementById('feedback').innerText = "太小了!";
} else if (guess > numberToGuess) {
document.getElementById('feedback').innerText = "太大了!";
} else {
document.getElementById('feedback').innerText = `恭喜你!答对了。你总共尝试了${attempts}次。`;
}
}
3. Java 版本(控制台应用)
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random random = new Random();
int numberToGuess = random.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("请输入你的猜测:");
if (scanner.hasNextInt()) {
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
} else {
System.out.println(String.format("恭喜你!答对了。你总共尝试了%d次。", attempts));
}
} else {
System.out.println("请输入一个有效的数字。");
scanner.next(); // 消耗掉错误的输入
}
}
scanner.close();
}
}
这些代码示例展示了如何用不同的编程语言实现一个简单的猜数字游戏。每个示例都遵循了相似的逻辑流程,但具体实现方式则根据各自语言的特性而有所不同。由于不同编程语言的特性和用途各异,我将为你提供一个简单的游戏概念——“猜数字”游戏,并用几种流行的编程语言来实现它。这个游戏的目标是让用户猜测一个由程序随机生成的数字。
- Python 版本
python
import random
def guess_number_game():
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"恭喜你!答对了。你总共尝试了{attempts}次。")
except ValueError:
print("请输入一个有效的数字。")
if name == “main”:
guess_number_game()
2. JavaScript 版本(适用于网页)
HTML 部分(index.html):
html
猜数字游戏
我已经选择了一个1到100之间的数字。请猜这个数字是多少:
猜测 JavaScript 部分(game.js):javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function guess() {
guess = parseInt(document.getElementById(‘guessInput’).value);
attempts++;
if (isNaN(guess)) {
document.getElementById('feedback').innerText = "请输入一个有效的数字。";
return;
}
if (guess < numberToGuess) {
document.getElementById('feedback').innerText = "太小了!";
} else if (guess > numberToGuess) {
document.getElementById('feedback').innerText = "太大了!";
} else {
document.getElementById('feedback').innerText = `恭喜你!答对了。你总共尝试了${attempts}次。`;
}
}
3. Java 版本(控制台应用)
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random random = new Random();
int numberToGuess = random.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("请输入你的猜测:");
if (scanner.hasNextInt()) {
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
} else {
System.out.println(String.format("恭喜你!答对了。你总共尝试了%d次。", attempts));
}
} else {
System.out.println("请输入一个有效的数字。");
scanner.next(); // 消耗掉错误的输入
}
}
scanner.close();
}
}
这些代码示例展示了如何用不同的编程语言实现一个简单的猜数字游戏。每个示例都遵循了相似的逻辑流程,但具体实现方式则根据各自语言的特性而有所不同。