【发布时间】:2016-05-27 18:44:28
【问题描述】:
您好,我是新来的,这是我的第一个问题: 我有一个带有简单算术运算符的代码。 但它不起作用:
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。我不明白为什么。 那么问题出在哪里?如何修复此错误?
【问题讨论】:
-
每当处理 short 和 int 算术运算时,java 在
int中返回结果,因此您必须将其类型转换为int -
premennaTypuShort -= 7; -
将其转换为
short而不是int抱歉打错了