Java - 无限递归

时间:2017-12-28 20:19:34

标签: java recursion

当试图运行这行代码时,temperatureInKelvin的初始化突出显示消息“无限递归”,我想不出任何解决方法。

double temperatureInKelvin(double temperatureInFahrenheight) {
    double fahrenheightToKelvinCalculation = (temperatureInFahrenheight - 32.0 * 5.0 / 9.0 + 273.15);
    return temperatureInKelvin(fahrenheightToKelvinCalculation);
}

1 个答案:

答案 0 :(得分:2)

您根本不需要重新致电temperatureInKelvin。只需返回您在fahrenheightToKelvinCalculation中计算的值即可。请注意,顺便说一句,你的括号是错误的,你应该在华氏值乘以5/9之前从华氏值中减去32:

double temperatureInKelvin(double temperatureInFahrenheight){
    return (temperatureInFahrenheight - 32.0) * 5.0 / 9.0 + 273.15;
}