2014-10-12 50 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