Python: 猜数字游戏
python
import random
def guess_number(): 58one.com
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('请输入一个整数!')
print(f'恭喜你,猜对了!数字是 {number_to_guess},你用了 {attempts} 次尝试。')
guess_number()
JavaScript: 简单的HTML页面中的猜数字游戏
HTML部分:
html
javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
function checkGuess() {
const guessInput = document.getElementById(‘guessInput’);
const feedback = document.getElementById(‘feedback’);
const guess = parseInt(guessInput.value);
if (isNaN(guess)) {
feedback.textContent = '请输入一个整数!';
return;
}
attempts++;
if (guess === numberToGuess) {
feedback.textContent = `恭喜你,猜对了!数字是 ${numberToGuess},你用了 ${attempts} 次尝试。`;
} else if (guess < numberToGuess) {
feedback.textContent = '太小了!';
} else {
feedback.textContent = '太大了!';
}
guessInput.value = ''; // 清空输入框
}
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;
int guess;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("猜一个1到100之间的数字: ");
guess = scanner.nextInt();
attempts++;
if (guess == numberToGuess) {
System.out.println("恭喜你,猜对了!数字是 " + numberToGuess + ",你用了 " + attempts + " 次尝试。");
break;
} else if (guess < numberToGuess) {
System.out.println("太小了!");
} else {
System.out.println("太大了!");
}
}
scanner.close();
}
}
C#: 简单的控制台猜数字游戏
csharp
using System;
class Program
{
static void Main()
{
Random rand = new Random();
int numberToGuess = rand.Next(1, 101);
int guess;
int attempts = 0;
while (true)
{
Console.Write("猜一个1到100之间的数字: ");
if (!int.TryParse(Console.ReadLine(), out guess))
{
Console.WriteLine("请输入一个整数!");
continue;
}
attempts++;
if (guess == numberToGuess)
由于各种电脑语言有其特定的用途和优势,一个简单的小程序游戏可以用多种语言来实现。以下,我将分别为你提供使用Python(通用脚本语言)、JavaScript(网页前端语言)、Java(通用面向对象编程语言)和C#(Windows应用程序开发常用语言)编写的简单控制台游戏的示例代码。
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('请输入一个整数!')
print(f'恭喜你,猜对了!数字是 {number_to_guess},你用了 {attempts} 次尝试。')
guess_number()
JavaScript: 简单的HTML页面中的猜数字游戏
HTML部分:
html
javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
function checkGuess() {
const guessInput = document.getElementById(‘guessInput’);
const feedback = document.getElementById(‘feedback’);
const guess = parseInt(guessInput.value);
if (isNaN(guess)) {
feedback.textContent = '请输入一个整数!';
return;
}
attempts++;
if (guess === numberToGuess) {
feedback.textContent = `恭喜你,猜对了!数字是 ${numberToGuess},你用了 ${attempts} 次尝试。`;
} else if (guess < numberToGuess) {
feedback.textContent = '太小了!';
} else {
feedback.textContent = '太大了!';
}
guessInput.value = ''; // 清空输入框
}
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;
int guess;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("猜一个1到100之间的数字: ");
guess = scanner.nextInt();
attempts++;
if (guess == numberToGuess) {
System.out.println("恭喜你,猜对了!数字是 " + numberToGuess + ",你用了 " + attempts + " 次尝试。");
break;
} else if (guess < numberToGuess) {
System.out.println("太小了!");
} else {
System.out.println("太大了!");
}
}
scanner.close();
}
}
C#: 简单的控制台猜数字游戏
csharp
using System;
class Program
{
static void Main()
{
Random rand = new Random();
int numberToGuess = rand.Next(1, 101);
int guess;
int attempts = 0;
while (true)
{
Console.Write("猜一个1到100之间的数字: ");
if (!int.TryParse(Console.ReadLine(), out guess))
{
Console.WriteLine("请输入一个整数!");
continue;
}
attempts++;
if (guess == numberToGuess)