Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
以下 Java 代码的输出是3.0.
3.0
为什么不是3.3333333...?
3.3333333...
double a = 10 / 3; System.out.println(a);
因为int / int返回一个int(不管你之后将它分配给什么)。
int / int
int
所以10 / 3返回3(整数除法向下舍入)。
10 / 3
3
这只会被转换为double.
double
要解决此问题,请将其中一个值设为 a double(所以它是double / int,它返回 a double):
double / int
double a = 10.0 / 3;
或者
double a = (double)10 / 3;