所以我正在尝试编写一个程序来将摄氏度转换为华氏度,反之亦然。每次我运行程序时,我都会遇到无法解释的错误。例如,它将 100 C 转换为 132 F。它将 212 F 转换为 0 C。我的转换公式是正确的。谁能给我一些线索?我尝试在主类之外的类中声明浮点数,但没有帮助。
import java.util.Scanner;
public class TempConverter{
public static void main(String [] args)
{
float f, c;
f = c = 0;
int a;
Scanner scan = new Scanner (System.in);
System.out.println("Press 1 for C->F or 2 for F->C");
a = scan.nextInt();
if (a == 1)
convertCtoFAndPrint();
else
convertFtoCAndPrint();
}
public static void convertFtoCAndPrint()
{
f = c = 0;
Scanner scan = new Scanner (System.in);
System.out.println("Please enter degrees F");
f = scan.nextFloat();
c = (5/9)*(f-32);
System.out.println(f + " degrees F is " + c + " degrees C.");
}
public static void convertCtoFAndPrint()
{
Scanner scan = new Scanner (System.in);
System.out.println("Please enter degrees C");
c = scan.nextFloat();
f = c*(9/5)+32;
System.out.println(c + " degrees C is " + f + " degrees F.");
}
}