调用方法有问题 [英] Trouble with calling method
本文介绍了调用方法有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public static void main (String[] args) {
for (double f=0; f<=20;f++) {
System.out.println(f+":Degrees Fahrenheit converted to Celsius = "+ celsius(f));
}
}
public static double celsius (double fahrenheit) {
double cels=(fahrenheit-32)/(5/9);
return cels;
}
通过这个程序,我正在尝试将华氏温度转换为摄氏温度,当我调用方法Celsius到我的主程序,当我运行程序时,Celsius的最终结果总是-infinity。我做错了什么?
With this program I'm trying to convert Fahrenheit to Celsius, and when I call method Celsius to my main when I run the program the end result for Celsius is always -infinity. What am I doing wrong?
推荐答案
你正在进行int division,它总是返回一个int。例如 5/9
返回0.
You're doing int division which always returns an int. For example 5/9
returns 0.
而是使用双重分割 5.0 / 9.0
对于您的代码:
public static double celsius (double fahrenheit) {
return (fahrenheit - 32) / (5.0 / 9.0);
}
我们应该将此问题视为重复,因为它被多次询问。例如: fahrenheit-to-celsius-conversion -yields-only-0-0-and-0-0
We should probably close this question as a duplicate as it's been asked many many times. For example: fahrenheit-to-celsius-conversion-yields-only-0-0-and-0-0
这篇关于调用方法有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文