gpt4 book ai didi

java - 调试模块中的逻辑错误

转载 作者:行者123 更新时间:2023-11-30 07:53:09 25 4
gpt4 key购买 nike

我在完成书中的作业时遇到问题。我正在编写一个 super 简单的应用程序,将华氏温度转换为摄氏度,反之亦然。一切工作都很完美,除了我编写的用于将华氏度转换为摄氏度的模块不断返回零。我无法弄清楚,我一直在 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;
}
}

最佳答案

我认为问题出在表达式 5/9 上。如果将两个整数相除,结果也是整数,在本例中为 0。尝试 5.0/9.0 * (f - 32)

关于java - 调试模块中的逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33057796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com