Java算术部门

public class test { public static void main(String[] args) { int total = 2; int rn = 1; double rnp = (rn / total) * 100; System.out.println(rnp); } } 

为什么它打印0.0而不是50.0?

https://www.google.com/search?q=100*(1%2F2)&aq=f&oq=100*(1%2F2)

除法发生在整数空间中,没有分数的概念,你需要类似的东西

 double rnp = (rn / (double) total) * 100 

你在这里调用整数除法

 (rn / total) 

整数除法向零舍入。

试试这个:

 double rnp = ((double)rn / total) * 100; 

在java和大多数其他编程语言中,当你划分两个整数时,结果也是一个整数。 剩下的就丢弃了。 因此, 1 / 2返回0 。 如果你想要返回一个floatdouble值,你需要做一些像1 * 1.0 / 2这样的东西,它将返回0.5 。 将整数乘以或除以double或float将其转换为该格式。

 public class test { public static void main(String[] args) { int total = 2; int rn = 1; double rnp = (rn / (float)total) * 100; System.out.println(rnp); } }