不能键入'诠释'隐式转换为“短” [英] Cannot implicitly convert type 'int' to 'short'

查看:106
本文介绍了不能键入'诠释'隐式转换为“短”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了下面的小程序,打印出Fibonacci序列:

I wrote the following small program to print out the Fibonacci sequence:

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();

}



编译消息是:

The compilation message is:

无法隐式转换类型'诠释'
以短。显式转换
存在(是否缺少强制转换?)

Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?)

由于所涉及的一切都是Int16的(短),那么为什么在那里进行的任何隐式转换?而更多的specificially为什么在这里失效(而不是在最初分配一个int的变量)?

Since everything involved is an Int16 (short) then why are there any implicit conversions going on? And more specificially why the failure here (and not when initially assigning an int to the variable)?

这是解释,将不胜感激。

An explanation would be much appreciated.

推荐答案

微软的的Int16 变量转换成的Int32 。做附加功能时

Microsoft converts your Int16 variables into Int32 when doing the add function.

更改如下:

Int16 answer = firstNo + secondNo;



到...

into...

Int16 answer = (Int16)(firstNo + secondNo);

这篇关于不能键入'诠释'隐式转换为“短”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆