- Python
Python 是一种广泛使用的高级编程语言,非常适合初学者。
python
import random
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print(“我已经想好了一个1到100之间的数字。来猜猜看吧!”) wiee.cn
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(“请输入一个有效的数字。”)
2. JavaScript (HTML + JavaScript)
JavaScript 常常与 HTML 和 CSS 一起用于网页开发。
HTML (index.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(‘guess’).value);
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 是一种静态类型、面向对象的编程语言,常用于企业级应用和Android应用开发。
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 = 0;
int attempts = 0;
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));
}
}
}
}
以上示例展示了如何用 Python、JavaScript(结合 HTML)和 Java 编写一个简单的猜数字游戏。每种语言都有其独特的语法和特性,但基本概念是相似的。由于篇幅限制,我将为你概述如何用几种不同的编程语言来创建一个简单的游戏:猜数字游戏。这个游戏将随机生成一个1到100之间的数字,然后玩家尝试猜测这个数字,直到猜对为止。
- Python
Python 是一种广泛使用的高级编程语言,非常适合初学者。
python
import random
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(“请输入一个有效的数字。”)
2. JavaScript (HTML + JavaScript)
JavaScript 常常与 HTML 和 CSS 一起用于网页开发。
HTML (index.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(‘guess’).value);
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 是一种静态类型、面向对象的编程语言,常用于企业级应用和Android应用开发。
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 = 0;
int attempts = 0;
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));
}
}
}
}
以上示例展示了如何用 Python、JavaScript(结合 HTML)和 Java 编写一个简单的猜数字游戏。每种语言都有其独特的语法和特性,但基本概念是相似的。