gpt4 book ai didi

java - float/int 隐式转换

转载 作者:IT老高 更新时间:2023-10-28 20:43:21 26 4
gpt4 key购买 nike

我正在做 floats 和 ints 的乘法和除法,我忘记了隐式转换规则(而且问题中的单词似乎太模糊,无法搜索更多比在这里问快)。

如果我有两个 int,但我想做浮点除法,我只需要转换一个或两个操作数吗?乘法怎么样 - 如果我将 floatint 相乘,答案是 float 吗?

最佳答案

您不能将 float 除以 int 的结果分配给 int,反之亦然。

所以答案是:

If I have two ints, but I want to do floating point division…?

一个 Actor 就足够了。

If I multiply a float and an int, is the answer a float?

是的。


float f = 1000f;
int i = 3;

f = i; // Ok
i = f; // Error

f = i/f; //Ok 0.003
f = f/i; //Ok 333.3333(3)

i = i/f; //Error
i = f/i; //Error

关于java - float/int 隐式转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4097694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com