有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java调试模块中的逻辑错误

我在书中的作业上遇到了麻烦。我正在编写一个超级简单的应用程序,将华氏温度转换为摄氏温度,反之亦然。除了我写的将华氏温度转换为摄氏温度的模块一直返回零外,其他一切都很完美。我搞不清楚,我一直在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) 个答案

  1. # 1 楼答案

    我想问题在于表达式5 / 9。如果你除以两个整数,结果也是整数,在本例中是0。试试5.0 / 9.0 * (f - 32)