我必须编写一个程序,记录2D阵列中5个不同商店的12个月利润。我让构造函数接受了利润的输入。当我尝试编译时,我的totalProfit方法存在问题。它说'double'不能被解除引用'并突出显示我的第一个for循环的.length部分。
import java.util.*;
public class Profits
{
static private double[][] profit=new double[5][12];
private Scanner in=new Scanner(System.in);
public static void main(String[] args){
System.out.println("Please input your profits, each month at a time.");
Profits year11=new Profits();
System.out.println(Arrays.deepToString(profit));
}
public Profits(){
for(int b=0; b<profit.length; b++){
for(int m=0; m<profit[0].length; m++){
profit[b][m]=in.nextDouble();
}
}
}
public double totalProfit(){
double profit=0.0;
for(int b=0; b<profit.length; b++){
for(int m=0; m<profit[0].length; m++){
profit+=profit[b][m];
}
}
return profit;
}
}
答案 0 :(得分:8)
您已声明类型为double
的局部变量,其名称与double[][]
数组相同。
double profit=0.0;
变量profit
现在隐藏了实例变量。
profit
- &gt;限定对this
数组的访问权限this.profit.length
,this.profit[0].length
- 只是为了完成答案。