- Python
python
import random
def guess_number_game(): hellohailuo.com
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 部分:
html
猜猜看,我想的是哪个数字?(1-100)
猜测<script src="guessGame.js"></script>
JavaScript 部分 (guessGame.js):
javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function checkGuess() {
guess = parseInt(document.getElementById(‘guessInput’).value, 10);
attempts++;
let feedback = document.getElementById('feedback');
if (guess < numberToGuess) {
feedback.textContent = "太低了!";
} else if (guess > numberToGuess) {
feedback.textContent = "太高了!";
} else {
feedback.textContent = `恭喜你!猜对了!数字就是${numberToGuess}。你总共尝试了${attempts}次。`;
}
}
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("请输入你的猜测:");
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太低了!");
} else if (guess > numberToGuess) {
System.out.println("太高了!");
} else {
System.out.println(String.format("恭喜你!猜对了!数字就是%d。你总共尝试了%d次。", numberToGuess, attempts));
}
}
scanner.close();
}
}
这些示例覆盖了三种非常不同的编程环境:Python(适用于脚本和应用程序开发),JavaScript(常用于网页和前端开发),以及Java(适用于大型应用程序和Android开发)。每种语言都有其独特的语法和特性,但基本逻辑在猜数字游戏中是相似的。由于直接给出所有电脑语言的示例代码不太现实(因为存在成百上千种编程语言),我将选择几种流行的编程语言来展示一个简单的游戏示例:猜数字游戏。这个游戏会随机生成一个1到100之间的数字,然后让玩家猜测这个数字是多少,直到猜对为止。
- 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 部分:
html
猜猜看,我想的是哪个数字?(1-100)
猜测<script src="guessGame.js"></script>
JavaScript 部分 (guessGame.js):
javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function checkGuess() {
guess = parseInt(document.getElementById(‘guessInput’).value, 10);
attempts++;
let feedback = document.getElementById('feedback');
if (guess < numberToGuess) {
feedback.textContent = "太低了!";
} else if (guess > numberToGuess) {
feedback.textContent = "太高了!";
} else {
feedback.textContent = `恭喜你!猜对了!数字就是${numberToGuess}。你总共尝试了${attempts}次。`;
}
}
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("请输入你的猜测:");
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太低了!");
} else if (guess > numberToGuess) {
System.out.println("太高了!");
} else {
System.out.println(String.format("恭喜你!猜对了!数字就是%d。你总共尝试了%d次。", numberToGuess, attempts));
}
}
scanner.close();
}
}
这些示例覆盖了三种非常不同的编程环境:Python(适用于脚本和应用程序开发),JavaScript(常用于网页和前端开发),以及Java(适用于大型应用程序和Android开发)。每种语言都有其独特的语法和特性,但基本逻辑在猜数字游戏中是相似的。