在Java中将华氏度转换为摄氏温度 [英] Converts a Fahrenheit degree to Celsius in java
问题描述
我正在尝试将华氏度转换为摄氏温度.现在当我使用此方法时出现错误:
I am trying converts a Fahrenheit degree to Celsius.Now I have a Error,When I use this:
double fahrenheit=input.nextDouble();
double celsius= ( **5** /9)*(fahrenheit-32);(Error)
然后我再次尝试给出(5.0),那是为什么呢?
Then again I am trying given (5.0) then it is work why?
推荐答案
值'5'和'9'是整数,而5/9被视为 0
而不是 0.55
,这就是为什么您会得到错误的答案.当两个整数相除时,答案自动为 floor()
.有必要将"5"和"9"之一写为5.0和9.0
The values '5' and '9' are integers and 5/9 is considered as 0
instead of 0.55
which is why you get the wrong answer. When two integers are divided, the answer is floor()
automatically. It is necessary to write either one of '5' and '9' as 5.0 and 9.0
一种更好的做法是始终同时写成 5.0
和 9.0
或写成 double(5)
和 double(9)
由于乘法和其他运算大部分都是递归的,因此该语句也可以正常工作.(fahrenheit-32)* 5/9;
,因为该语句以双精度值开头
A better practice is to always write both as 5.0
and 9.0
or write them as double(5)
and double(9)
Since multiplication and other operations are mostly left recursive, this statement would work fine as well.
(fahrenheit-32)*5/9;
as the statement starts with a double value
这篇关于在Java中将华氏度转换为摄氏温度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!