如何修复Java中的“无法取消引用双精度”

时间:2019-04-10 23:34:39

标签: java

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

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

我在参数集(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);

   }


}

1 个答案:

答案 0 :(得分:0)

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

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的任何理由。