华氏摄氏度转换 [英] Fahrenheit to Celsius conversion

查看:136
本文介绍了华氏摄氏度转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,将以华氏度表示的温度转换为摄氏度。用户输入华氏值,程序打印出摄氏度值。用户应该输入212作为华氏值,程序应该计算100.0作为摄氏度值,但是当我想要获得100.0

I'm trying to write a program that converts temperatures expressed in degree Fahrenheit to degree Celsius. The user enters a Fahrenheit value and the program prints out the Celsius value. The user is supposed to enter 212 as the Fahrenheit value the program is supposed to calculate 100.0 as the Celsius value but instead I'm getting 0.0 as the Celsius value when I want to get 100.0

我不知道问题可能在哪里。操作的顺序是否有可能?

I'm not sure where the problem might be. Is it possibility the order of operations?

将摄氏度转换为华氏度的公式是: C = 5/9(F - 32)

The formula for converting Celsius to Fahrenheit is : C = 5 / 9 (F - 32)

import acm.program.*; 

public class FahrenheitToCelsius extends ConsoleProgram {

    public void run() {
        println("This program converts Fahrenheit to Celsius");
        int fah = readInt("Enter Fahrenheit temperature: ");
        double ft = 5 / 9;
        double cel = ft * (fah - 32);
        println("Celsius equivalent = " + cel);
    }
}


推荐答案

code> 5/9 是一个整数除法,所以它会返回一个 0 即使你把它分配给一个 double

尝试 5.0 / 9

5 / 9 is an integer division, so it'll return a 0 even though you're assigning it to a double.
Try 5.0 / 9.

这篇关于华氏摄氏度转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆