我又寫道在C#下面的代碼,它說不能ulong類型隱式轉換爲INT我能做些什麼來糾正,爲什麼會出現這種情況C#。不能隱式轉換的int ULONG
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'。 – wablab
由於'ulong'是* unsigned *和'int'是* signed *,因此不清楚如何轉換* negative *值。你真的想要'超長',而不是'長'嗎? –
對不起,但我不相信你這個錯誤出現在你顯示的代碼片段中,因爲沒有任何賦值或類似從'ulong'到'int'的那個。 _但是你有一些其他的錯誤:'Console.ReadLine()'返回一個'字符串'而不是'ulong',所以你不能分配它'input'。 –