嗨,我是新来的人,这是我的第一个问题:
我有一个带有简单算术运算符的代码。
但这行不通:

int a = 10;
short premennaTypuShort = (short)a;     /*retyped variable a into short type and entered into new variable*/
premennaTypuShort = premennaTypuShort - 7;
/*now i am trying to decrease the number but eclipse wrote that cannot convert from int to short.*/


我试图减少指定为short的数字,但是eclipse写道,它不能从int转换为short。我不明白为什么。
那么问题出在哪里呢?我该如何修复此错误?

最佳答案

java是32位。这意味着无论何时执行任何算术运算,它都将返回32位值。因此,您必须再次将其强制转换为短路,如下所示:

int a = 10;
short premennaTypuShort = (short)a;
premennaTypuShort =(short)(premennaTypuShort - 7);

10-08 15:57