我一直在Java中“双重无法取消引用”

时间:2022-12-13 21:46:51

I have to write a program that records 12 months of profit for 5 different shops in a 2D array. I made the constructor to take the input of the profits. I when I try to compile, there's an issue in my totalProfit method. It says 'double cannot be dereferenced' and highlight the .length part of my first for loop.

我必须编写一个程序,记录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;
   }

}

1 个解决方案

#1


8  

You have declared a local variable of type double with the same name as double[][] array.

您已声明一个double类型的局部变量,其名称与double [] []数组相同。

double profit=0.0;

The variable profit now hides the instance variable.

变量利润现在隐藏了实例变量。

  • Either change the name of the variable - preferable.
  • 要么改变变量的名称 - 更好。

  • Or qualify the access to profit array with this -> this.profit.length, this.profit[0].length - just to complete the answer.
  • 或者使用这个 - > this.profit.length,this.profit [0] .length来限定对利润数组的访问 - 只是为了完成答案。

#1


8  

You have declared a local variable of type double with the same name as double[][] array.

您已声明一个double类型的局部变量,其名称与double [] []数组相同。

double profit=0.0;

The variable profit now hides the instance variable.

变量利润现在隐藏了实例变量。

  • Either change the name of the variable - preferable.
  • 要么改变变量的名称 - 更好。

  • Or qualify the access to profit array with this -> this.profit.length, this.profit[0].length - just to complete the answer.
  • 或者使用这个 - > this.profit.length,this.profit [0] .length来限定对利润数组的访问 - 只是为了完成答案。