小编ppr*_*rav的帖子

x = x*0.90; 给出有损转换错误。x*=0.90;才不是。为什么?

我已经写了代码:

int x = 18;
x *= 0.90; 
System.out.println(x);
Run Code Online (Sandbox Code Playgroud)

这段代码打印出来16

然而,当我写下

int x = 18;
x = x * 0.90; 
System.out.println(x);
Run Code Online (Sandbox Code Playgroud)

它给了我以下错误:incompatible types: possible lossy conversion from double to int

我预计这两个代码示例都会导致与 相同的错误x *= y;x = x * y;x *= 0.90;不知何故有效,但x = x * 0.90;无效。为什么会这样呢?

java double integer multiplication assignment-operator

25
推荐指数
2
解决办法
2489
查看次数