[JAVA]方法将F转换为C [英] [JAVA] Method convert F to C
本文介绍了[JAVA]方法将F转换为C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,我又遇到另一个问题,我收到-0.0 20次,实际上是19次,即使我需要20次
Hello everyone, I am back yet with another question, I receive -0.0 20 times, well actually 19 , even though I need 20
double temp=20;
for(int index = 1; index < temp; index++)
{
celsius(index);
}
}
public static double celsius(double temperature)
{
double celsius;
celsius = (5/9) * ( temperature - 32);
System.out.println(celsius);
return celsius;
}
推荐答案
这是因为5/9不正确.这是整数除法,当分子小于分母时始终返回0.
如果仅更改乘法顺序,则第一个整数操作数将隐式转换为double;否则,将转换为double.然后将所有其他操作数转换为double,因为第一个操作数temperature
被声明为double;您会得到正确的结果:
This is because 5/9 is incorrect. This is integer division, always returns 0 when a numerator is less than denominator.
If you simply change the order of multiplication, first integer operand will implicitly convert to double; and then all other operands will be converted to double, as the first operandtemperature
is declared double; and you will get a correct result:
celsius = (temperature - 32) * 5 / 9;
请参阅: http://en.wikipedia.org/wiki/Celsius [
See: http://en.wikipedia.org/wiki/Celsius[^].
这篇关于[JAVA]方法将F转换为C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文