C#Do While循环问题

时间:2012-02-13 18:05:11

标签: c#

我是编码的新手,我真的坚持这个C#do while循环业务。经过长时间的搞乱之后,我终于让它循环了一个声明,但现在它只是重复了先发表的任何声明。例如,如果它产生数字6并且你猜7它会说“太高......”但是当你再次猜测你是否太低或等于6时,它仍然会说“太高......” 。“

谢谢

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

        int intRandomNum;

        // ask the random num gen for num between 1 and 12
        intRandomNum = r.Next(10) + 1;

        //ask user for their first guess
        Console.WriteLine("I'm going to generate a random number, can you guess what it is? " + intRandomNum);

        int intUserNum = int.Parse(Console.ReadLine());

        do
        {

            if (intUserNum == intRandomNum)
            {
                Console.WriteLine("You got it! Great job!");
            }

            if (intUserNum < intRandomNum)
            {
                Console.WriteLine("Too low! Try Again.");
                Console.ReadLine();
            }
            if (intUserNum > intRandomNum)
            {
                Console.WriteLine("Too high! Try again.");
                Console.ReadLine();
            }

        } while (intUserNum != intRandomNum);

        Console.ReadKey();
    }
}

}

感谢大家的帮助!我知道了!终于来了!

7 个答案:

答案 0 :(得分:5)

您需要将解析放在循环中,以便重新计算intUserNum

    int intUserNum = int.Parse(Console.ReadLine());

    do
    {

        if (intUserNum == intRandomNum)
        {
            Console.WriteLine("You got it! Great job!");
        }

        if (intUserNum < intRandomNum)
        {
            Console.WriteLine("Too low! Try Again.");
            intUserNum = int.Parse(Console.ReadLine());
        }
        if (intUserNum > intRandomNum)
        {
            Console.WriteLine("Too high! Try again.");
            intUserNum = int.Parse(Console.ReadLine());
        }

    } while (intUserNum != intRandomNum);

您编写的代码会从用户处读取一个新行,但它永远不会将其转换为数字,因此您的变量永远不会更改。这会导致您的第一个错误猜测永远使用。

请注意,使用int.TryParse代替int.Parse也可能是个好主意。这允许您处理用户输入的不是整数的东西......

答案 1 :(得分:1)

将输入读数移动到循环内:

int intUserNum;

    do
    {
        intUserNum = int.Parse(Console.ReadLine());
        if (intUserNum == intRandomNum)
        {
            Console.WriteLine("You got it! Great job!");
        }

        if (intUserNum < intRandomNum)
        {
            Console.WriteLine("Too low! Try Again.");
        }
        if (intUserNum > intRandomNum)
        {
            Console.WriteLine("Too high! Try again.");
        }

    } while (intUserNum != intRandomNum);

答案 2 :(得分:1)

这是因为实际从用户获取数字到intUserNum的代码,即

int intUserNum = int.Parse(Console.ReadLine());

外面循环,所以它只会被执行一次。每次循环重复时,它都会检查相同的旧数字。

编码提示:请记住,计算机不知道您打算要做什么。您的意图可能是正确的,即intUserNum应始终包含用户猜测的下一个数字。但是,计算机只执行您告诉它的操作,并且您告诉它用户只应输入一次 - 在循环内部,您永远不会说intUserNum应该采用新值,因此它将会永远保留其旧价值。

答案 3 :(得分:0)

int intUserNum = int.Parse(Console.ReadLine());

应位于循环的顶部。

不知道C#但应该这样做。

答案 4 :(得分:0)

您必须获取并解析循环中的输入。否则值始终保持不变:

// ...

do
{
    int intUserNum = int.Parse(Console.ReadLine());

    if (intUserNum == intRandomNum)
        Console.WriteLine("You got it! Great job!");

    if (intUserNum < intRandomNum)
    {
        Console.WriteLine("Too low! Try Again.");
        Console.ReadLine();
    }
    if (intUserNum > intRandomNum)
    {
        Console.WriteLine("Too high! Try again.");
        Console.ReadLine();
    }

} while (intUserNum != intRandomNum);

答案 5 :(得分:0)

static void Main(string[] args)
{
    var rnd = new Random();
    var n = rnd.Next(13);

    while (!Guess(n)) ;

    Console.ReadKey();
}

static bool Guess(int n)
{
    int input;

    if (!int.TryParse(Console.ReadLine(), out input))
        return false;

    var msg = input == n ? "Win" : input < n ? "Low" : "High";
    Console.WriteLine(msg);

    return input == n;
}

答案 6 :(得分:0)

我有一个与我自己类似的问题,并且正在为几乎两个全班课程而苦苦挣扎,直到我找到你的答案(讽刺的是)在输入我自己的问题的过程中。

{
    class Program
    {

        static void Main(string[] args)
        {
            string aKnight = "Romulus";
            string bKnight = "Remus";
            string Weapon;


            Console.WriteLine($"{aKnight} implies {bKnight} is unorthodox.");
            /*Console.ReadLine();*/

            Console.WriteLine($"{bKnight} deicdes to hit {aKnight}. What should he use? Club, Bow, or Tactical Nuke?");
            /*Console.ReadLine();*/


            do
            {
                Weapon = Console.ReadLine();
                switch (Weapon)
                {

                    case "Club":
                        Console.WriteLine("Works for me.");
                        Console.WriteLine($"{bKnight} knocks {aKnight} out with the club.");
                        Console.ReadLine();
                        break;

                    case "Bow":
                        Console.WriteLine("Eh, creative yet acceptable.");
                        Console.WriteLine($"{bKnight} hits {aKnight} over the head with the bow. It just hurt a little.");
                        Console.ReadLine();
                        break;

                    case "Tactical Nuke":
                        Console.WriteLine("Are.. Are you serious? For the love of... JUST WHY?!");
                        Console.WriteLine("TACTICAL NUKE INBOUND!!!... It lagged...");
                        Console.ReadLine();
                        break;

                    default:
                        Console.Clear();
                        Console.WriteLine("Please make a choice.");
                        //Console.ReadLine();
                        break;

                }
            } while (Weapon != default && Weapon != "Club" && Weapon != "Bow" && Weapon != "Tactical Nuke");

                Console.WriteLine("What will happen next ?");
                Console.ReadLine();
        }
    }
}

我需要做的就是将Weapon = Console.ReadLine()移动到do / while括号中并添加(&amp;&amp;&amp; Weapon!=&#34; Club&#34;&amp;&amp;&amp; Weapon!=&# 34; Bow&#34;&amp;&amp; Weapon!=&#34; Tactical Nuke&#34;)进入括号。非常感谢! :d