调试模块中的逻辑错误

时间:2015-10-10 18:59:35

标签: java eclipse debugging

我的书中的作业遇到了麻烦。我正在写一个超级简单的应用程序,将华氏温度转换为celcius,反之亦然。一切都很完美,除了我写的将华氏转换为celcius的模块不断返回零。我无法弄清楚,我一直在Eclipse中进行调试,逐步完成每一步,我无法弄清楚我的f> c转换方程有什么问题,或者它可能是什么。任何比我更有经验的人都能看到问题吗?我真的很感激一下。非常感谢!

import java.util.Scanner;

public class TemperatureConversions 
{

    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        String initialType;
        double fahrenheit = 1;
        double celcius = 1;
        System.out.print("Would you like to enter a fahrenheit or celcius temperature for conversion? (f/c):  ");
        initialType = input.next();
        switch ( initialType )
        {
            case "f":
                System.out.printf("Please input fahrenheit temperature:  ");
                fahrenheit = input.nextDouble();
                celcius = celcius (fahrenheit);
                System.out.print("\n" + fahrenheit + "f converted to celcius is " +celcius + "c");
                break;
            case "c":
                System.out.printf("Please input celcius temperature:  ");
                celcius = input.nextInt();
                fahrenheit = fahrenheit (celcius);
                System.out.print("\n" + celcius + "c converted to fahrenheit is " + fahrenheit + "f");
                break;
            default:
                System.out.printf("%s is an invalid input.  Please try again, and only input either 'f' or 'c'");
                break;
        }
        System.out.printf("\nAdios Turd Nuggets");
    }

    public static double celcius(double f)
    {
        double c = 5 / 9 * (f - 32);
        return c;
    }

    public static double fahrenheit(double c)
    {
        double f = c * 1.8 + 32;
        return f;
    }
}

1 个答案:

答案 0 :(得分:1)

我认为问题出在表达式5 / 9上。如果你除了两个整数,结果也是整数,在这种情况下它是0.尝试5.0 / 9.0 * (f - 32)