public class TempTabl {
public static void main (String[] args) {
double a = 0;
double b = 0;
double c = 0;
double z = 0;
System.out.printf(" Temperature | Temperature%n");
System.out.printf(" (degrees) | (degrees) %n");
System.out.printf(" F C | C F%n");
z = 0;
while (a <= 450) {
b = fahrenheit(a,b);
a = -40 + (5 * z);
System.out.printf(" %-10s %4f| %-10s %4f", a, b, a, c);
System.out.println("");
z++;
}
}
public static double fahrenheit(double a,double b)
{
b = ((a-32)*5)/9;
return b;
}
public static double celsius(double z,double c)
{
c = -40 + ( 9 * z);
return c;
}
}
我得到了什么:
Temperature |
(degrees) |
F C |
-40.0 -17.777778|
-35.0 -40.000000|
-30.0 -37.222222|
我期望得到什么:
温度 |
(度)|
F C |
−40.000 −40.000 |
−35.000 −37.222 |
−30.000 −34.444 |
解决办法是: 在 while 循环内 当您调用华氏度时,参数“a”为零,因此您应该在调用华氏度函数之前定义“a” 这将修复它,您将得到预期的输出