【问题标题】:Why won't my variable change within my method?为什么我的变量不会在我的方法中改变?
【发布时间】:2016-03-17 19:29:03
【问题描述】:

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

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;
   } 
}

【问题讨论】:

    标签: java loops methods


    【解决方案1】:

    您的方法celsius(farenheit) 只被调用一次, 之后你做一个循环,增加华氏温度但不再调用该方法......

    您必须在 while 循环内移动 double celsius = celsius(farenheit);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-04
      • 2019-08-11
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      相关资源
      最近更新 更多