为什么我的变量不会在我的方法中改变?

时间:2016-03-17 19:29:04

标签: java loops methods

我使用循环为华氏温度在0到20之间制作华氏转换表,我无法让我的方法返回正确的摄氏温度。到目前为止,该程序将输出适当的华氏温度20,但它会输出0 * farenheit的摄氏度转换为每个farenheit输出。如何让我的方法在方法的方程中使用增加的farenheit变量? 这是我到目前为止的代码:

import java.text.DecimalFormat;

public class CelsiusTemperatureTable
{
   public static void main(String[] args)
   {
      DecimalFormat df = new DecimalFormat("#.0");

      double farenheit = 0;
      double celsius = celsius(farenheit);


      while (farenheit <= 20)
      {

         System.out.println( farenheit + "\t\t" + df.format(celsius));

         farenheit++;
      }
   }

   public static double celsius(double farenheit)
   {
      double temperature = farenheit - 32;
      double temperature1 = temperature * .556;
      return temperature1;
   } 
}

1 个答案:

答案 0 :(得分:1)

您的方法celsius(farenheit)只被调用一次, 之后你做了一个循环,增加了farenheit但没有再调用这个方法......

你必须在while循环中移动double celsius = celsius(farenheit);