我有这段代码,变量 s
的输出始终为零:
double v = high; double s; double h;
if (v == 0) {
s = 0;
h = 0;
System.out.println("v and s and h is zeroo" + v + s + h);
} else {
s = (high - low) / high;
System.out.println("s is equal to" + s);
System.out.println("high is equal to" + high);
System.out.println("low is equal to" + low);
if (s == 0) {
h = 0;
System.out.println("high and low are equals" + high + "==" + low);
} else {
double alpha;
alpha = 60 * (mid - low) / (high - low);
System.out.println("alpha is : " + alpha);
}
}
输出示例:
s is equal to0.0
high is equal to139
low is equal to30
high and low are equals139==30
最佳答案
由于 high
和 low
都是 int
类型,因此 s = (high-low)/行中的除法high
使用整数除法,并且由于 high - low
始终小于 high
(除非 low 是负数),因此结果始终为零。
要解决此问题,请将其中一个转换为 double:
s = (high-low)/(double)high;
关于java - 无论输入是什么,操作的输出始终为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26058140/