Java 调试模块中的逻辑错误
我在书中的作业上遇到了麻烦。我正在编写一个超级简单的应用程序,将华氏温度转换为摄氏温度,反之亦然。除了我编写的将华氏温度转换为摄氏温度的模块一直返回零外,所有的工作都很完美。我搞不清楚,我一直在Eclipse中调试,一步一步地完成每一步,我搞不清楚我的f>c转换方程到底出了什么问题,或者它可能是什么。有没有比我更有经验的人能看到这个问题?我真的很想看一下。非常感谢Java 调试模块中的逻辑错误,java,eclipse,debugging,Java,Eclipse,Debugging,我在书中的作业上遇到了麻烦。我正在编写一个超级简单的应用程序,将华氏温度转换为摄氏温度,反之亦然。除了我编写的将华氏温度转换为摄氏温度的模块一直返回零外,所有的工作都很完美。我搞不清楚,我一直在Eclipse中调试,一步一步地完成每一步,我搞不清楚我的f>c转换方程到底出了什么问题,或者它可能是什么。有没有比我更有经验的人能看到这个问题?我真的很想看一下。非常感谢 import java.util.Scanner; public class TemperatureConversions {
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;
}
}
我想问题出在表达式
5/9
上。如果除以两个整数,结果也是整数,在本例中为0。试试5.0/9.0*(f-32)
谢谢!我试试看。如果行得通,请接受这个答案-