控制台为什么不返回双精度值?

问题描述 投票:0回答:1

这是作业:(编写一个程序,该程序从控制台读取一个双精度学位,然后将其转换为华氏度并向用户显示结果。转换的公式为:华氏=(9/5)*摄氏+ 32)。问题是我似乎无法获得十进制数字,只能获得四舍五入的版本。

这是我的代码:

package JavaLearningSections;

import java.util.Scanner;

public class Assignment1Nr3 {

    public static void main (String[]args) {

        System.out.println("Enter the temperature in degree to turn it to Fahrenheit");

        Scanner input = new Scanner(System.in);

        double numDegree = input.nextDouble();

        double numFahrenheit = (9/5 *numDegree)+32;

        System.out.println(numFahrenheit);

    }


}
java double
1个回答
0
投票
原因是因为您正在5/9上进行整数除法,这将返回整数。为了获得双倍,您需要将5转换为双倍,或将9转换为双倍,如下所示:

double numFahrenheit = ((double)9/5 * numDegree) + 32;

© www.soinside.com 2019 - 2024. All rights reserved.