- Python 版本cztaihubay.com.cn
python
import random
number_to_guess = random.randint(1, 100)
guess = None
while guess != number_to_guess:
try:
guess = int(input("Guess a number between 1 and 100: "))
if guess < number_to_guess:
print(“Too low!”)
elif guess > number_to_guess:
print(“Too high!”)
else:
print(“Congratulations, you guessed it right!”)
except ValueError:
print(“Please enter a valid number.”)
2. JavaScript (浏览器环境)
HTML 部分(index.html):
html
Guess the Number Game
Guess! JavaScript 部分(game.js):javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess;
let feedback = document.getElementById(‘feedback’);
function checkGuess() {
guess = parseInt(document.getElementById(‘guess’).value, 10);
if (isNaN(guess)) {
feedback.textContent = “Please enter a valid number.”;
} else if (guess < numberToGuess) {
feedback.textContent = “Too low!”;
} else if (guess > numberToGuess) {
feedback.textContent = “Too high!”;
} else {
feedback.textContent = “Congratulations, you guessed it right!”;
}
}
3. Java 版本 (简单命令行界面)
Java 版本通常涉及到更复杂的设置,比如使用 Scanner 类来读取输入,但这里是一个简化的框架:
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;
Scanner scanner = new Scanner(System.in);
int guess;
while (true) {
System.out.print("Guess a number between 1 and 100: ");
if (scanner.hasNextInt()) {
guess = scanner.nextInt();
if (guess < numberToGuess) {
System.out.println("Too low!");
} else if (guess > numberToGuess) {
System.out.println("Too high!");
} else {
System.out.println("Congratulations, you guessed it right!");
break;
}
} else {
System.out.println("That's not a valid number!");
scanner.next(); // this is important!
}
}
scanner.close();
}
}
以上示例展示了如何用 Python、JavaScript 和 Java 编写一个简单的猜数字游戏。每种语言都有其独特的语法和库,但基本的逻辑结构是相似的。由于直接在这里提供完整的、多语言版本的小游戏代码可能会非常冗长,我将为你概述一个简单的猜数字游戏(Guess the Number Game)的几种不同编程语言实现的基本框架。这个游戏的基本规则是:程序随机选择一个数字(例如1到100之间),用户尝试猜测这个数字,程序会告诉用户他们的猜测是太高、太低还是正确。
- Python 版本
python
import random
number_to_guess = random.randint(1, 100)
guess = None
while guess != number_to_guess:
try:
guess = int(input("Guess a number between 1 and 100: "))
if guess < number_to_guess:
print(“Too low!”)
elif guess > number_to_guess:
print(“Too high!”)
else:
print(“Congratulations, you guessed it right!”)
except ValueError:
print(“Please enter a valid number.”)
2. JavaScript (浏览器环境)
HTML 部分(index.html):
html
Guess the Number Game
Guess! JavaScript 部分(game.js):javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess;
let feedback = document.getElementById(‘feedback’);
function checkGuess() {
guess = parseInt(document.getElementById(‘guess’).value, 10);
if (isNaN(guess)) {
feedback.textContent = “Please enter a valid number.”;
} else if (guess < numberToGuess) {
feedback.textContent = “Too low!”;
} else if (guess > numberToGuess) {
feedback.textContent = “Too high!”;
} else {
feedback.textContent = “Congratulations, you guessed it right!”;
}
}
3. Java 版本 (简单命令行界面)
Java 版本通常涉及到更复杂的设置,比如使用 Scanner 类来读取输入,但这里是一个简化的框架:
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;
Scanner scanner = new Scanner(System.in);
int guess;
while (true) {
System.out.print("Guess a number between 1 and 100: ");
if (scanner.hasNextInt()) {
guess = scanner.nextInt();
if (guess < numberToGuess) {
System.out.println("Too low!");
} else if (guess > numberToGuess) {
System.out.println("Too high!");
} else {
System.out.println("Congratulations, you guessed it right!");
break;
}
} else {
System.out.println("That's not a valid number!");
scanner.next(); // this is important!
}
}
scanner.close();
}
}
以上示例展示了如何用 Python、JavaScript 和 Java 编写一个简单的猜数字游戏。每种语言都有其独特的语法和库,但基本的逻辑结构是相似的。