Java - 开尔文到华氏度的转换方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22059486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Kelvin to Fahrenheit conversion method
提问by Alzecha
So I'm currently working on a pretty simple program. What it does is convert either Fahrenheit or Celsius to Kelvin, and then depending on what the user requests (f or c) that kelvin value is converted and returned as either Celsius or Fahrenheit.
所以我目前正在开发一个非常简单的程序。它的作用是将华氏度或摄氏度转换为开尔文,然后根据用户请求(f 或 c)将开尔文值转换为摄氏度或华氏度并返回。
My Celsius conversion seems to work just fine, but Fahrenheit is a different story. Our professor says our output must match the given example 100%, and when I give a Celsius value and request Celsius back, it always returns the value I originally put in.
我的摄氏度转换似乎工作得很好,但华氏度是一个不同的故事。我们的教授说我们的输出必须与给定的示例 100% 匹配,当我给出摄氏度值并请求返回摄氏度时,它总是返回我最初输入的值。
However, I got this when converting 95 Celsius to Fahrenheit: 203.28800000000007 The value I was supposed to get was: 203.0 Furthermore, when I put in 50 Fahrenheit and requested it to return Fahrenheit, I got this: 32.0.
但是,我在将 95 摄氏度转换为华氏度时得到了这个:203.28800000000007 我应该得到的值是:203.0 此外,当我输入 50 华氏度并要求它返回华氏度时,我得到了这个:32.0。
I'll post the class that contains all my conversion methods, but could anyone help me out with where I might be going wrong? It looks to me like based on my formula, it's simply returning the part of the formula that adds/subtracts 32. I've tried alternative formats of the formula, but nothing seems to be working.
我将发布包含我所有转换方法的类,但有人可以帮我解决可能出错的地方吗?在我看来,根据我的公式,它只是返回了加/减 32 的公式部分。我尝试了公式的替代格式,但似乎没有任何效果。
public class Temperature
{
// Instance variable
private double degreesKelvin; // degrees in Kelvin
// Constructor method: initialize degreesKelvin to zero
public Temperature()
{
degreesKelvin = 0;
}
// Convert and save degreesCelius in the Kelvin scale
public void setCelsius(double degreesCelsius)
{
degreesKelvin = degreesCelsius + 273.16;
}
// Convert degreesKelvin to Celsius and return the value
public double getCelsius()
{
double c = degreesKelvin - 273.16;
return c;
}
// Convert and save degreesFahrenheit in the Kelvin scale
public void setFahrenheit(double degreesFahrenheit)
{
degreesKelvin = (5/9 * (degreesFahrenheit - 32) + 273);
}
// Convert degreesKelvin to Fahrenheit and return the value
public double getFahrenheit()
{
double f = (((degreesKelvin - 273) * 9/5) + 32);
return f;
}
}
Thanks for any assistance, I tried looking for solutions to this issue, but nothing seems to work for me so far.
感谢您的任何帮助,我尝试寻找解决此问题的方法,但到目前为止似乎没有任何效果。
采纳答案by Josh M
Watch out for integer division, the result of 2 integers (when divided) yields an integer:
注意整数除法,2 个整数的结果(除法时)产生一个整数:
5/9 = 0
9/5 = 1
To fix this, cast 1 of them to a floating type, like:
要解决此问题,请将其中的 1 个转换为浮动类型,例如:
5d/9 //or 5.0/9
And likewise with
同样与
9d/5 //or 9.0/5
回答by SameeraGupta
The problem is happening because while storing you are adding 273.16 to the degree celsius. The extra .16 introduces this error. for 95 degree celsiun
问题正在发生,因为在存储时您将 273.16 添加到摄氏度。额外的 .16 引入了这个错误。95 摄氏度
degreeKelvin = 273.16+95
开尔文度数 = 273.16+95
now while returning it becomes
现在返回时它变成
5/9*(273.16+95-273) +32 == 5/9(95.16)+32 == 203.2
5/9*(273.16+95-273) +32 == 5/9(95.16)+32 == 203.2
while
尽管
5/9*(273+95-273) + 32 == 203
5/9*(273+95-273) + 32 == 203