随机猜数字游戏 [英] random number guessing game

查看:47
本文介绍了随机猜数字游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个随机数字猜谜游戏,计算机认为该数字介于1到100之间.然后,它询问您是什么,并告诉您是对还是错.但是,无论什么时候调试,它都会说它出于某种原因高于或低于实际的随机数.另外,它会立即说出其中两个陈述.另外,我不确定该怎么说该人进行了多少猜测.这是我失败的代码.

I am making a random number guessing game which the computer thinks of a number between 1-100. It then asks you what it is and tells you if you are right or wrong. However, whenever I debug, it says that it is higher or lower than the actual random number for some reason. Plus, it says two of those statements at once. Also, I'm not sure how to say how many guesses the person has taken. Here is my unsuccessful code.

static void Main(string[] args) 
{
    Random random = new Random();

    int returnValue = random.Next(1, 100);
    int Guess = 0;

    Console.WriteLine("I am thinking of a number between 1-100.  Can you guess what it is?");

    while (Guess != returnValue)
    {
        Guess = Convert.ToInt32(Console.Read());

        while (Guess < returnValue)
        {
            Console.WriteLine("No, the number I am thinking of is higher than " + Guess + " .  Can you guess what it is?");
            Console.ReadLine();

        }
        while (Guess > returnValue)
        {
            Console.WriteLine("No, the number I am thinking of is lower than " + Guess + " .  Can you guess what it is");
            Console.ReadLine();
        }
    }
    while (Guess == returnValue)
    {
        Console.WriteLine("Well done! The answer was " + returnValue);
        Console.ReadLine();
    }
}

推荐答案

您正在使用不需要迭代的很多.while语句就像IF语句一样具有布尔条件.

You're using a lot of unneeded iteration. The while statement takes a boolean condition just like an IF statement.

static void Main(string[] args)

{

Random random = new Random();

int returnValue = random.Next(1, 100);

        int Guess = 0;

        Console.WriteLine("I am thinking of a number between 1-100.  Can you guess what it is?");

        while (Guess != returnValue)
        {
            Guess = Convert.ToInt32(Console.Read());

            if (Guess < returnValue)
            {
                Console.WriteLine("No, the number I am thinking of is higher than " + Guess + ". Can you guess what it is?");
            }
            else if (Guess > returnValue)
            {
                Console.WriteLine("No, the number I am thinking of is lower than " + Guess + ". Can you guess what it is?");
            }

        }

        Console.WriteLine("Well done! The answer was " + returnValue);
        Console.ReadLine();

}

这篇关于随机猜数字游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆