我编写了以下小程序来打印斐波那契数列:
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 分配给变量时)?
一个解释将不胜感激。