首页 > 解决方案 > 如何在 Java 中修复“无法取消引用双精度”

问题描述

我试图将双精度和双精度数组作为我的方法的参数,但是当我调用这些方法时出现错误,“无法取消引用双精度”。

我尝试了不同的语法,例如 var.method(array[]); , var.method(数组);

我在参数集上尝试了多种语法,(double[] array), (double array[]);

public class Rainfall extends rainfallTest
{   

     private double total;
     private double Average;


     //total rainfall for the year
     public double totalRain(double[] rain){

     for (int index = 0; index < rain.length; index++){
         total += rain[index];
     }

     return total;

    }//end totalRain

    //calculating the monthly average
    public double monthlyAvg(double totalRain){

      Average = totalRain / 12.0;
      return Average;

    }

    //calculating the month with the most rain
    public double mostRain(double[] rain){

      double highest = rain[0];
      for (int index = 1; index < rain.length; index++){
          if (rain[index] > highest){
          highest = rain[index];
          }
      }
     return highest;    
    }

    public double leastRain(double[] rain){

      double lowest = rain[0];
      for (int index = 1; index < rain.length; index++){
         if (rain[index] < lowest){
            lowest = rain[index];
         }

      }
      return lowest;
    }
 }

和测试程序:

public class rainfallTest{



   public static void main(String[] args){

      double rain[] = {2.2, 5.2, 1.0, 10.2, 3.2, 9.2, 5.2, 0.0, 9.9, 12.2, 5.2, 2.2};
      double Average;
      double total;
      double most;
      double least;


      System.out.println("Here's the rainfall for this year");

      total.totalRain(rain);
      Average.monthlyAvg(total);
      most.mostRain(rain);
      least.leastRain(rain);

      System.out.println("The total rainfall for the year is: " + total +
                         ". the monthly average of rain is: " + Average + 
                         ". The highest rain in one month: " + most +
                         ". The lowest amount of rain in one month: " + least);

   }


}

标签: java

解决方案


你没有正确调用你的方法。首先,您需要一个类的实例:

Rainfall rainfall = new Rainfall();

然后您可以调用该实例上的方法,并将返回值分配给您的变量:

double total = rainfall.totalRain(rain);
double average = rainfall.monthlyAvg(total);
double most = rainfall.mostRain(rain);
double least = rainfall.leastRain(rain);

此外,这不是一个大问题,但我看不出有任何理由Rainfall来扩展rainfallTest.


推荐阅读