Python
python
import random
def game(): qianxiong.com.cn
secret_num = random.randint(1, 100)
guess = None
attempt = 0
while guess != secret_num:
guess = int(input("猜一个1到100之间的数字: "))
attempt += 1
if guess < secret_num:
print("太小了!")
elif guess > secret_num:
print("太大了!")
print(f"恭喜你,你猜对了!数字是 {secret_num},你尝试了 {attempt} 次。")
if name == “main”:
game()
JavaScript (浏览器端)
HTML:
html
<script src="game.js"></script>
JavaScript (game.js):
javascript
let secretNum = Math.floor(Math.random() * 100) + 1;
let guess;
let attempts = 0;
function checkGuess() {
guess = parseInt(document.getElementById(‘guess’).value);
attempts++;
if (guess === secretNum) {
document.getElementById('message').textContent = `恭喜你,你猜对了!数字是 ${secretNum},你尝试了 ${attempts} 次。`;
} else if (guess < secretNum) {
document.getElementById('message').textContent = "太小了!";
} else {
document.getElementById('message').textContent = "太大了!";
}
}
Java (控制台应用)
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random rand = new Random();
int secretNum = rand.nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
int guess;
int attempts = 0;
while (true) {
System.out.print("猜一个1到100之间的数字: ");
guess = scanner.nextInt();
attempts++;
if (guess == secretNum) {
System.out.printf("恭喜你,你猜对了!数字是 %d,你尝试了 %d 次。\n", secretNum, attempts);
break;
} else if (guess < secretNum) {
System.out.println("太小了!");
} else {
System.out.println("太大了!");
}
}
scanner.close();
}
}
这些示例代码分别用Python、JavaScript和Java编写了一个简单的“猜数字”游戏。每个程序都包含了一个随机生成的数字,玩家需要猜测这个数字,并根据猜测的结果给出反馈。由于篇幅限制,我将为您提供几种不同编程语言中非常简单的“猜数字”游戏的代码示例。这个游戏的基本规则是计算机随机选择一个数字,然后玩家尝试猜测这个数字。
Python
python
import random
def game():
secret_num = random.randint(1, 100)
guess = None
attempt = 0
while guess != secret_num:
guess = int(input("猜一个1到100之间的数字: "))
attempt += 1
if guess < secret_num:
print("太小了!")
elif guess > secret_num:
print("太大了!")
print(f"恭喜你,你猜对了!数字是 {secret_num},你尝试了 {attempt} 次。")
if name == “main”:
game()
JavaScript (浏览器端)
HTML:
html
<script src="game.js"></script>
JavaScript (game.js):
javascript
let secretNum = Math.floor(Math.random() * 100) + 1;
let guess;
let attempts = 0;
function checkGuess() {
guess = parseInt(document.getElementById(‘guess’).value);
attempts++;
if (guess === secretNum) {
document.getElementById('message').textContent = `恭喜你,你猜对了!数字是 ${secretNum},你尝试了 ${attempts} 次。`;
} else if (guess < secretNum) {
document.getElementById('message').textContent = "太小了!";
} else {
document.getElementById('message').textContent = "太大了!";
}
}
Java (控制台应用)
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random rand = new Random();
int secretNum = rand.nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
int guess;
int attempts = 0;
while (true) {
System.out.print("猜一个1到100之间的数字: ");
guess = scanner.nextInt();
attempts++;
if (guess == secretNum) {
System.out.printf("恭喜你,你猜对了!数字是 %d,你尝试了 %d 次。\n", secretNum, attempts);
break;
} else if (guess < secretNum) {
System.out.println("太小了!");
} else {
System.out.println("太大了!");
}
}
scanner.close();
}
}
这些示例代码分别用Python、JavaScript和Java编写了一个简单的“猜数字”游戏。每个程序都包含了一个随机生成的数字,玩家需要猜测这个数字,并根据猜测的结果给出反馈。