下面我将为您提供使用Python、JavaScript (可以在浏览器中运行)、Java和C#编写的简单“猜数字”游戏代码。

本文详细介绍了如何用Python、JavaScript、Java和C#这四种编程语言实现一个基本的猜数字游戏,涉及随机数生成和用户交互的代码片段。

当然可以。下面我将为您提供使用Python、JavaScript (可以在浏览器中运行)、Java和C#编写的简单“猜数字”游戏代码。

Python
python
import random  
  
def guess_number():  
    number_to_guess = random.randint(1, 100)  
    guess = None  
    num_guesses = 0  
  
    while guess != number_to_guess:  
        guess = int(input('Guess a number between 1 and 100: '))  
        num_guesses += 1  
        if guess < number_to_guess:  
            print('Too low!')  
        elif guess > number_to_guess:  
            print('Too high!')  
      
    print(f'Congratulations, you guessed the number in {num_guesses} guesses!')  
  
guess_number()
JavaScript (可在浏览器中运行)
javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;  
let numGuesses = 0;  
  
function guessNumber() {  
    let guess = parseInt(prompt('Guess a number between 1 and 100:'), 10);  
    numGuesses++;  
    if (guess < numberToGuess) {  
        alert('Too low!');  
    } else if (guess > numberToGuess) {  
        alert('Too high!');  
    } else {  
        alert(`Congratulations, you guessed the number in ${numGuesses} guesses!`);  
    }  
}  
  
// Start the game by calling the function  
guessNumber();
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 numGuesses = 0;  
        Scanner scanner = new Scanner(System.in);  
        int guess = 0;  
  
        while (guess != numberToGuess) {  
            System.out.print("Guess a number between 1 and 100: ");  
            guess = scanner.nextInt();  
            numGuesses++;  
            if (guess < numberToGuess) {  
                System.out.println("Too low!");  
            } else if (guess > numberToGuess) {  
                System.out.println("Too high!");  
            } else {  
                System.out.println("Congratulations, you guessed the number in " + numGuesses + " guesses!");  
            }  
        }  
        scanner.close();  
    }  
}
C#
csharp
using System;  
  
class GuessNumberGame  
{  
    static void Main()  
    {  
        Random rand = new Random();  
        int numberToGuess = rand.Next(1, 101); // Random number between 1 and 100  
        int numGuesses = 0;  
        int guess = 0;  
#chhas{
margin-top: 50px;
padding:itxyz.cn;
font-size: 18px;
cursor: 10px 20px;
}
        while (guess != numberToGuess)  
        {  
            Console.Write("Guess a number between 1 and 100: ");  
            guess = Convert.ToInt32(Console.ReadLine());  
            numGuesses++;  
            if (guess < numberToGuess)  
            {  
                Console.WriteLine("Too low!");  
            }  
            else if (guess > numberToGuess)  
            {  
                Console.WriteLine("Too high!");  
            }  
            else  
            {  
                Console.WriteLine($"Congratulations, you guessed the number in {numGuesses} guesses!");  
            }  
        }  
    }  
}
以上代码分别展示了如何使用Python、JavaScript、Java和C#来实现一个简单的猜数字游戏。这些游戏都会生成一个1到100之间的随机数,并让用户不断猜测,直到猜中为止。

评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值