小编MAH*_*ESH的帖子

错误:不兼容的类型:从int到short的可能有损转换.我不知道为什么我收到此错误消息

public class Demo 
{
    public static void main(String[] args) 
    {
       int a=10,b=20;
       short c = (a<b)?a:b;
       System.out.println(c);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的程序,我得到以下错误,为什么我没有得到

"Demo.java:6: error: incompatible types: possible lossy conversion from int to short
short c = (a<b)?a:b;
1 error" 
Run Code Online (Sandbox Code Playgroud)

我用变量声明写"final",它工作正常.但为什么会这样呢?

public class Demo 
{
    public static void main(String[] args) 
    {
        final int a=10,b=20;
        short c = (a<b)?a:b;
        System.out.println(c);
    }
}
Run Code Online (Sandbox Code Playgroud)

java

5
推荐指数
1
解决办法
343
查看次数

标签 统计

java ×1