Java程序错误,将华氏温度转换为摄氏温度

时间:2014-05-01 18:20:10

标签: java math floating-point type-conversion jtextfield

我遇到了java程序问题,Java E6B飞行计算机突然停止将华氏温度转换为摄氏温度,它总是输出0,无论什么值,Celsius到Fahrenheit的编写类似并且工作正常,两个函数都写入一个日志文件,这是有效的,除了华氏温度到摄氏温度,日志文件中的任何输入都为0。 (我知道我拼错了Fahrenheit)。我考虑过改为Double而不是浮动

这是两个函数的代码。还包括舍入函数。

public static String celstofare(String Celsius){

    // function level
    float a;
    // if user types string, returns error
    try
    {
        a = Float.parseFloat(Celsius);
                    float math = ((a * (9/5)) + 32);
                    String out = round(math, 2) +"";
                    // log file update mechanism
                            File file = new File("log.txt");
                            FileWriter writer;
                            try {
                                writer = new FileWriter(file, true);
                                PrintWriter printer = new PrintWriter(writer);
                                Date date = new Date();
                                String strDate = date.toString();
                                printer.println("Converted Celsius to Farenheit  on: " + strDate);
                                printer.println("Celsius: " + a + "Farenheit: " + out);
                                printer.close();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
        return out;
    }
    catch (Exception e)
    {
        return "error";
    }

}

    public static String faretocels(String Farenheit){

    // function level
    float a;
    // if user types string, returns error
    try
    {
        a = Float.parseFloat(Farenheit);
                    float math = ((a - 32) * (5/9));
                    String out = round(math, 2) +"";
                    // log file update mechanism
                            File file = new File("log.txt");
                            FileWriter writer;
                            try {
                                writer = new FileWriter(file, true);
                                PrintWriter printer = new PrintWriter(writer);
                                Date date = new Date();
                                String strDate = date.toString();
                                printer.println("Converted Farenheit to Celsius on: " + strDate);
                                printer.println("Farenheit: " + a + "Celsius: " + out);
                                printer.close();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
        return out;
    }
    catch (Exception e)
    {
        return "error";
    }

}
    public static BigDecimal round(double d, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Double.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);       
    return bd;
}

这里是调用它的函数和动作监听器,其他所有东西都可以工作,并且我已经验证了正确的输入和输出是否已设置。输入和输出框是JTextFields

        else if (e.getSource() == btnCalculateConversion){
                    mphOut.setText(Conversions.KnotstoMph(knots.getText()));
                    knotsOut.setText(Conversions.mphtoknots(mph.getText()));
                    nmout.setText(Conversions.mphtoknots(mph.getText()));
                    miout.setText(Conversions.KnotstoMph(knots.getText()));
                    poundsout.setText(Conversions.galtopound(gal.getText()));
                    galout.setText(Conversions.poundtogal(pounds.getText()));
                    tempfout.setText(Conversions.celstofare(tempc.getText()));
                    tempcout.setText(Conversions.faretocels(tempf.getText()));

3 个答案:

答案 0 :(得分:1)

float math = ((a * (9/5)) + 32);

9/5被评估为整数除法,或1. a * 9 / 5可以避免此问题,因为评估是从左到右。

奇怪的是,这应该导致每次a + 32的温度,而不是0。因此可能意味着Celsius正在parseFloat中评估为0。您是否尝试单步执行调试器,以查看a的值是什么?

答案 1 :(得分:1)

因为在从华氏温度转换为摄氏温度时执行5/9除法部分时,Java认为它是纯整数除法,并且会向下舍入为零,然后假设在从原始温度减去32之后乘以零。 / p>

解决方案是强制Java进行浮点除法,这可以通过执行5.0 / 9,5 / 9.0甚至5.0 / 9.0之类的操作来实现。然后,Java将确保不会进行舍入为零,并且您的答案不会为零,除非它需要。

答案 2 :(得分:1)

所有的答案都非常好,但是自从你是学生以来我还会再添加一件事:

这种方法应该做一件事而且只做一件事:将温度从Farenheit转换为Celcius。就是这样。不写文件或其他任何东西。在方法之外做那件事。

为什么你要进出弦乐?气温是数字;使用它们。

了解Sun Java编码标准。变量名以小写字母开头,类名以大写字母开头。

所以你的方法应该是这样的:

public class TemperatureConverter {

    private TemperatureConverter() {}

    public static double celciusToFarenheit(double celcius) {
        return 1.8*celcius + 32.0;    
    }

    public static double farenheitToCelcius(double farenheit) {
        return (farenheit - 32.0)/1.8;
    }
}