我在c#中使用了以下代码,它说不能将ulong类型隐式转换为int我该怎么做才能纠正以及为什么会这样

 Random rnd = new Random();

        ulong a;
        ulong input;
        int c1 = 0;
        int c2;

        a = (ulong)rnd.Next(1, 101);

        Console.WriteLine("Welcome to the random number checker.\n"
            +"You can guess the number. Try and find in how many tries you can get it right. "
            +"\n\t\t\t\tGame Start");

        do
        {
            Console.WriteLine("Enter your guess");
            input = Console.ReadLine();
            c1 = c1 + 1;
            c2 = c1 + 1;
            if (input == a)
            {
                Console.WriteLine("CONGRATZ!!!!.You got that correct in "+c1
                    + "tries");
                c1 = c2;

            }
            else if (input > a)
            {
                Console.WriteLine("You guessed the number bit too high.try again ");
            }
            else
            {
                Console.WriteLine("You guessed the number bit too low ");
            };
        } while (c1 != c2);


每当我删除该do{}部分时,上层程序就可以正常工作,但是随着我的添加,它就会显示出该问题。

最佳答案

我编译您的代码只有一个错误:

Cannot implicitly convert type 'string' to 'ulong'


排队

input = Console.ReadLine();


如果将其更改为:

input = Convert.ToUInt64(Console.ReadLine());


一切都会好起来的

10-02 18:49