且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java中的整数除法

更新时间:2023-12-03 23:45:28

转换输出为时已晚;计算已经在整数算术中进行了.您需要将 inputs 转换为 double:

Converting the output is too late; the calculation has already taken place in integer arithmetic. You need to convert the inputs to double:

System.out.println((double)completed/(double)total);

请注意,您实际上不需要转换两个的输入.只要其中一个是double,另一个就会被隐式转换.但为了对称,我更喜欢两者兼而有之.

Note that you don't actually need to convert both of the inputs. So long as one of them is double, the other will be implicitly converted. But I prefer to do both, for symmetry.