我寫了下面的小程序,打印出的斐波那契序列:不能類型「詮釋」隱式轉換爲「短」
static void Main(string[] args)
{
Console.Write("Please give a value for n:");
Int16 n = Int16.Parse(Console.ReadLine());
Int16 firstNo = 0;
Int16 secondNo = 1;
Console.WriteLine(firstNo);
Console.WriteLine(secondNo);
for (Int16 i = 0; i < n; i++)
{
//Problem on this line
Int16 answer = firstNo + secondNo;
Console.WriteLine(answer);
firstNo = secondNo;
secondNo = answer;
}
Console.ReadLine();
}
編譯的消息是:
無法隱式轉換類型' int' 改爲'short'。一個顯式轉換 存在(是否缺少強制轉換?)
由於所涉及的一切都是Int16的(短),那麼爲什麼會有怎麼回事任何隱式轉換?更具體地說,爲什麼在這裏失敗(而不是最初將一個int分配給變量)?
解釋將不勝感激。
什麼是錯誤?你爲什麼用短褲出於興趣? – 2011-05-04 11:15:42
@湯姆id假設他有評論的行這個行上的問題 – Manatherin 2011-05-04 11:16:28
相關:http://stackoverflow.com/questions/1084668/why-is-the-result-of-a-subtraction-of-an-int16 int16-variable-a – SquidScareMe 2011-05-04 11:19:52