java - 如何在 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);
}
}
解决方案
你没有正确调用你的方法。首先,您需要一个类的实例:
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
.
推荐阅读
- javascript - 搜索表单刷新页面而不是搜索值
- reactjs - 如何从异步函数返回 redux 状态?
- firebase - 如何在 kotlin 中自定义相机
- c# - smtp通过添加多个到vs异步方法发送多个邮件
- ngrx-store - 如何修复:ngRx/entity 中的“TypeError:entities is not iterable”
- pandas - 自定义标签的 xtics 位置
- flutter - 基于 Flutter 设置中字体大小的自适应字体大小
- vue.js - VueJS 中 Prop 的默认选项
- apk - 如何调试为什么我的渐进式 Web 应用 PWA 不会转换为 android apk
- java - 使用 ImageIO.read() 生成的图像是否仍使用传递给它的相同内存?