Python
python
import random
def guess_number(): hy029.cn
secret_number = random.randint(1, 100)
guess = None
attempts = 0
print("欢迎来到猜数字游戏!")
print("我已经想好了一个1到100之间的数字,你能猜到吗?")
while guess != secret_number:
try:
guess = int(input("请输入你的猜测:"))
attempts += 1
if guess < secret_number:
print("太小了!")
elif guess > secret_number:
print("太大了!")
except ValueError:
print("请输入一个有效的数字!")
print(f"恭喜你,你猜对了!数字是 {secret_number},你一共猜了 {attempts} 次。")
if name == “main”:
guess_number()
JavaScript (在HTML中运行)
HTML:
html
javascript
function guessNumber() {
let secretNumber = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
const gameDiv = document.getElementById(‘game’);
gameDiv.innerHTML = "欢迎来到猜数字游戏!<br>我已经想好了一个1到100之间的数字,你能猜到吗?";
function updateGame() {
guess = prompt("请输入你的猜测:");
attempts++;
if (guess < secretNumber) {
gameDiv.innerHTML += "<p>太小了!</p>";
} else if (guess > secretNumber) {
gameDiv.innerHTML += "<p>太大了!</p>";
} else {
gameDiv.innerHTML += `<p>恭喜你,你猜对了!数字是 ${secretNumber},你一共猜了 ${attempts} 次。</p>`;
return;
}
if (isNaN(guess)) {
gameDiv.innerHTML += "<p>请输入一个有效的数字!</p>";
}
updateGame(); // 继续游戏,直到猜对
}
updateGame(); // 开始游戏
}
guessNumber();
Java (简单的控制台应用)
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random random = new Random();
int secretNumber = random.nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
int guess = 0;
int attempts = 0;
System.out.println("欢迎来到猜数字游戏!");
System.out.println("我已经想好了一个1到100之间的数字,你能猜到吗?");
while (guess != secretNumber) {
System.out.print("请输入你的猜测:");
if (scanner.hasNextInt()) {
guess = scanner.nextInt();
attempts++;
if (guess < secretNumber) {
System.out.println("太小了!");
} else if (guess > secretNumber) {
System.out.println("太大了!");
}
} else {
System.out.println("请输入一个有效的数字!");
scanner.next(); // 消耗掉非法输入
}
}
System.out.printf("恭喜你,你猜对了!数字是 %d,你一共猜了 %d 次。%n", secretNumber, attempts);
scanner.close();
}
}
由于篇幅限制,我将为你提供三种不同编程语言(Python、JavaScript 和 Java)的简单“猜数字”游戏代码示例。
Python
python
import random
def guess_number():
secret_number = random.randint(1, 100)
guess = None
attempts = 0
print("欢迎来到猜数字游戏!")
print("我已经想好了一个1到100之间的数字,你能猜到吗?")
while guess != secret_number:
try:
guess = int(input("请输入你的猜测:"))
attempts += 1
if guess < secret_number:
print("太小了!")
elif guess > secret_number:
print("太大了!")
except ValueError:
print("请输入一个有效的数字!")
print(f"恭喜你,你猜对了!数字是 {secret_number},你一共猜了 {attempts} 次。")
if name == “main”:
guess_number()
JavaScript (在HTML中运行)
HTML:
html
javascript
function guessNumber() {
let secretNumber = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
const gameDiv = document.getElementById(‘game’);
gameDiv.innerHTML = "欢迎来到猜数字游戏!<br>我已经想好了一个1到100之间的数字,你能猜到吗?";
function updateGame() {
guess = prompt("请输入你的猜测:");
attempts++;
if (guess < secretNumber) {
gameDiv.innerHTML += "<p>太小了!</p>";
} else if (guess > secretNumber) {
gameDiv.innerHTML += "<p>太大了!</p>";
} else {
gameDiv.innerHTML += `<p>恭喜你,你猜对了!数字是 ${secretNumber},你一共猜了 ${attempts} 次。</p>`;
return;
}
if (isNaN(guess)) {
gameDiv.innerHTML += "<p>请输入一个有效的数字!</p>";
}
updateGame(); // 继续游戏,直到猜对
}
updateGame(); // 开始游戏
}
guessNumber();
Java (简单的控制台应用)
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random random = new Random();
int secretNumber = random.nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
int guess = 0;
int attempts = 0;
System.out.println("欢迎来到猜数字游戏!");
System.out.println("我已经想好了一个1到100之间的数字,你能猜到吗?");
while (guess != secretNumber) {
System.out.print("请输入你的猜测:");
if (scanner.hasNextInt()) {
guess = scanner.nextInt();
attempts++;
if (guess < secretNumber) {
System.out.println("太小了!");
} else if (guess > secretNumber) {
System.out.println("太大了!");
}
} else {
System.out.println("请输入一个有效的数字!");
scanner.next(); // 消耗掉非法输入
}
}
System.out.printf("恭喜你,你猜对了!数字是 %d,你一共猜了 %d 次。%n", secretNumber, attempts);
scanner.close();
}
}