我得到我返回的循环的最后一个值,但我想要返回所有的值。我的输出如下所示:如何将循环中的值返回主方法?
Celsius Fahrenheit | Fahrenheit Celsius
40.0 87.80 | 120.00 -1.11
39.0 87.80 | 110.00 -1.11
38.0 87.80 | 100.00 -1.11
37.0 87.80 | 90.00 -1.11
36.0 87.80 | 80.00 -1.11
35.0 87.80 | 70.00 -1.11
34.0 87.80 | 60.00 -1.11
33.0 87.80 | 50.00 -1.11
32.0 87.80 | 40.00 -1.11
31.0 87.80 | 30.00 -1.11
我需要这些其他值来完成我的输出表。
public class TemperatureConverter {
public static void main(String[] args) {
double celsius = 0;
double fahrenheit = 0;
int v = 0;
double a = 0;
double b = 0;
double fahrenheitToCelsius = fahrenheitToCelsius(a);
double celsiusToFahrenheit = celsiusToFahrenheit(b);
System.out.println("Celsius Fahrenheit | Fahrenheit Celsius ");
celsius = 40;
fahrenheit = 120.0;
while (celsius >= 31.0) {
while (fahrenheit >= 30) {
System.out.printf("%-9.1f%6.2f | %9.2f%9.2f \n", celsius, celsiusToFahrenheit, fahrenheit, fahrenheitToCelsius);
celsius = celsius - 1;
fahrenheit = fahrenheit - 10;
}
}
celsiusToFahrenheit(celsius);
fahrenheitToCelsius(fahrenheit);
}
public static double celsiusToFahrenheit(double celsius) {
double fahrenheit = 0;
for (celsius = 40.0; celsius >= 31.0; celsius--) {
fahrenheit = ((9.0/5.0) * celsius + 32);
}
return fahrenheit;
}
public static double fahrenheitToCelsius(double fahrenheit) {
double celsius = 0;
for (fahrenheit = 120; fahrenheit >= 30; fahrenheit = fahrenheit - 10) {
celsius = ((5.0/9) * (fahrenheit - 32));
}
return celsius;
}
}
输出:
Celsius Fahrenheit | Fahrenheit Celsius
40.0 87.80 | 120.00 -1.11
39.0 87.80 | 110.00 -1.11
38.0 87.80 | 100.00 -1.11
37.0 87.80 | 90.00 -1.11
36.0 87.80 | 80.00 -1.11
35.0 87.80 | 70.00 -1.11
34.0 87.80 | 60.00 -1.11
33.0 87.80 | 50.00 -1.11
32.0 87.80 | 40.00 -1.11
31.0 87.80 | 30.00 -1.11
这 - '而(华氏> = 30){ System.out.printf( “% - 9.1f%6.2f |%9.2f%9.2f \ n” 个,摄氏温度,摄氏温度,华氏温度,华氏温度)。 celsius = celsius - 1;华氏温度=华氏温度-10℃; }'将'celsius = celsius - 1;'移到内循环之外。 – Leron 2014-10-12 07:20:38