- Python(猜数字游戏)dxwaiyan.cn
python
import random
def guess_number():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
try:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < number_to_guess:
print('太小了!')
elif guess > number_to_guess:
print('太大了!')
except ValueError:
print('请输入一个整数!')
continue
print(f'恭喜你,你猜对了!数字是 {number_to_guess},你尝试了 {attempts} 次。')
if name == “main”:
guess_number()
2. JavaScript(在浏览器控制台中的猜数字游戏)
由于JavaScript通常在浏览器中运行,你可以将以下代码粘贴到浏览器的开发者工具的控制台(Console)中运行。
javascript
function guessNumber() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
while (guess !== numberToGuess) {
guess = prompt('猜一个1到100之间的数字:');
attempts++;
if (guess < numberToGuess) {
console.log('太小了!');
} else if (guess > numberToGuess) {
console.log('太大了!');
}
}
console.log(`恭喜你,你猜对了!数字是 ${numberToGuess},你尝试了 ${attempts} 次。`);
}
guessNumber();
3. Java(简单的控制台猜数字游戏)
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;
Scanner scanner = new Scanner(System.in);
int guess = 0;
int attempts = 0;
while (guess != numberToGuess) {
System.out.print("猜一个1到100之间的数字: ");
if (scanner.hasNextInt()) {
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
} else {
System.out.println("请输入一个整数!");
scanner.next(); // 消耗非整数的输入
}
}
System.out.printf("恭喜你,你猜对了!数字是 %d,你尝试了 %d 次。\n", numberToGuess, attempts);
}
}
这些示例都是简单的猜数字游戏,你可以根据需要进行修改和扩展。由于篇幅限制,我无法为所有电脑语言提供完整的游戏代码,但我可以为你提供几种常见编程语言(如Python、JavaScript、Java)的简单小游戏代码示例,通常是控制台游戏。
- Python(猜数字游戏)
python
import random
def guess_number():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
try:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < number_to_guess:
print('太小了!')
elif guess > number_to_guess:
print('太大了!')
except ValueError:
print('请输入一个整数!')
continue
print(f'恭喜你,你猜对了!数字是 {number_to_guess},你尝试了 {attempts} 次。')
if name == “main”:
guess_number()
2. JavaScript(在浏览器控制台中的猜数字游戏)
由于JavaScript通常在浏览器中运行,你可以将以下代码粘贴到浏览器的开发者工具的控制台(Console)中运行。
javascript
function guessNumber() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
while (guess !== numberToGuess) {
guess = prompt('猜一个1到100之间的数字:');
attempts++;
if (guess < numberToGuess) {
console.log('太小了!');
} else if (guess > numberToGuess) {
console.log('太大了!');
}
}
console.log(`恭喜你,你猜对了!数字是 ${numberToGuess},你尝试了 ${attempts} 次。`);
}
guessNumber();
3. Java(简单的控制台猜数字游戏)
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;
Scanner scanner = new Scanner(System.in);
int guess = 0;
int attempts = 0;
while (guess != numberToGuess) {
System.out.print("猜一个1到100之间的数字: ");
if (scanner.hasNextInt()) {
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
} else {
System.out.println("请输入一个整数!");
scanner.next(); // 消耗非整数的输入
}
}
System.out.printf("恭喜你,你猜对了!数字是 %d,你尝试了 %d 次。\n", numberToGuess, attempts);
}
}
这些示例都是简单的猜数字游戏,你可以根据需要进行修改和扩展。