2014-10-12 43 views
-2

我得到我返回的循环的最后一个值,但我想要返回所有的值。我的输出如下所示:如何将循环中的值返回主方法?

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 
+0

这 - '而(华氏> = 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

回答

0

这是一些示例代码。注意嵌套的方法调用。你不应该循环你的转换方法。

public class TemperatureConverter { 

    public static double celsiusToFahrenheit(double celsius) { 
    return 9.0/5 * celsius + 32; 
    } 

    public static double fahrenheitToCelsius(double fahrenheit) { 
    return 5.0/9 * (fahrenheit - 32)); 
    } 

    public static void main(String[] args) { 
    double celsius = 40; 
    double fahrenheit = 120.0; 
    System.out.println("Celsius Fahrenheit\t| Fahrenheit Celsius "); 
    while (celsius > 30) { 
     System.out.printf("%-10.1f%6.2f\t|%8.2f%12.2f\n", celsius, 
      celsiusToFahrenheit(celsius), fahrenheit, fahrenheitToCelsius(fahrenheit)); 
     celsius--; 
     fahrenheit -= 10; 
    } 
    } 
} 

打印: Celsius Fahrenheit | Fahrenheit Celsius 40.0 104.00 | 120.00 48.89 39.0 102.20 | 110.00 43.33 38.0 100.40 | 100.00 37.78 37.0 98.60 | 90.00 32.22 36.0 96.80 | 80.00 26.67 35.0 95.00 | 70.00 21.11 34.0 93.20 | 60.00 15.56 33.0 91.40 | 50.00 10.00 32.0 89.60 | 40.00 4.44 31.0 87.80 | 30.00 -1.11

+0

非常感谢。我有我的代码工作。 :) – 2014-10-12 08:18:08

0

我不知道如果我的理解这个问题,但我认为你不需要celsiusToFahrenheitfahrenheitToCelsius方法里面for循环,你已经通过号码。

也许如果你改变的方法就是这样,它应该工作

public static double celsiusToFahrenheit (double celsius) { 
    double fahrenheit = 0; 
    fahrenheit = ((9.0/5.0) * celsius + 32); 
    return fahrenheit; 
} 

而且你必须将呼叫转移到这种方法的主循环里面!

+0

没有一个正在改变的变量,返回值将始终保持不变。在这种情况下,57.60。这是因为唯一的变量,摄氏度是恒定的。 – 2014-10-12 07:39:48

0

呼叫的同时

System.out.printf里面的方法( “% - 9.1f%6.2f |%9.2f%9.2f \ n”,摄氏度,celsiusToFahrenheit(摄氏),飞轮海,fahrenheitToCelsius(华氏));

+0

我相信这会导致我调用方法内的方法。 – 2014-10-12 07:44:45

+0

是的,你可以做到这一点,最内层的方法是从左到右进行处理。 – nmore 2014-10-12 07:56:45

+0

当我尝试它使一切都炸毁(不工作)。嗯.... idk。 – 2014-10-12 08:05:59