我用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 {}
部分,上面的程序就可以正常工作,但是一旦添加它,问题就出现了。
input = Console.ReadLine();
完全不应该编译通过;Console.ReadLine()
返回的是一个字符串string
,而不是无符号长整型ulong
。 - wablabulong
是 无符号 的,而int
是 有符号 的,因此如何转换 负数 值不清楚。你真的想要ulong
,而不是long
吗? - Dmitry Bychenkoulong
赋值给int
或类似操作。但是,您存在其他一些错误:Console.ReadLine()
返回的是string
而不是ulong
,所以您不能将其赋值给input
。 - René Vogta
与input
比较,它们都是ulong
,将c1
与c2
比较,它们都是int
。 - René Vogta
误认成了c
:| - Mats391