Java 我在一个子类中创建了一个多维Double数组(Double[][]),我需要在父类中打印这个数组

Java 我在一个子类中创建了一个多维Double数组(Double[][]),我需要在父类中打印这个数组,java,arrays,methods,Java,Arrays,Methods,我所有的主要方法都发生在这个类中: package wk2individual; import java.util.Scanner; public class Wk2Individual { public static void main(String[] args) { AnnualPayCalculator aPC = new AnnualPayCalculator(); SalesPerson sP = new Sal

我所有的主要方法都发生在这个类中:

package wk2individual;
    import java.util.Scanner;

    public class Wk2Individual {
        public static void main(String[] args) {   
        AnnualPayCalculator aPC = new AnnualPayCalculator();
        SalesPerson sP = new SalesPerson(); 
        //System greeting    
          Scanner sc = new Scanner (System.in);  
            System.out.println ("Welcome to the Employee Annual Pay calculator!");

        //user input
            System.out.println("Please enter the name of the first sales employee:");
                sP.salesPerson1 = sc.next();    

            System.out.println ("Please enter " + sP.salesPerson1 + "'s total sales for the year:"); 
                aPC.totalSales1 = sc.nextDouble();

        //begin outputs    
            if (aPC.totalSales1 >= 112000 && aPC.totalSales1 < 140000) {
                System.out.println(sP.salesPerson1 + " has earned $" + aPC.total1() + " in "
                        + "commissions for the year! " + sP.salesPerson1 + "'s total pay for the "
                        + "year will be $" + aPC.total2()); //outputs employees commission and pay if sales meet incentive
            }
            else if (aPC.totalSales1 >= 140000) {
                System.out.println(sP.salesPerson1 + " has earned $" + aPC.total3() + " in "
                        + "commissions for the year! " + sP.salesPerson1 + "'s total pay for the "
                        + "year will be $" + aPC.total4()); //outputs employees commission and pay if sales exceed targetSales
            }
            else if (aPC.totalSales1 < 112000) {
                System.out.println(sP.salesPerson1 + " will receive a total pay of $" + 
                        aPC.fixedSalary + " for the year. " + sP.salesPerson1 + " did not meet "
                        + "the sales incentive to earn commission for the year."); /*outputs employees end of year pay as fixed 
    salary since the sales amount is less than 80% of the sales target*/
            }

        //begin the inputs for the second salesperson        
            System.out.println("Now let's get the name of the second sales employee:");
                sP.salesPerson2 = sc.next();

            System.out.println("Please enter " + sP.salesPerson2 + "'s total sales for the year:");
                aPC.totalSales2 = sc.nextDouble();

            //begin outputs    
            if (aPC.totalSales2 >= 112000 && aPC.totalSales2 < 140000) {
                System.out.println(sP.salesPerson2 + " has earned $" + aPC.total5() + " in "
                        + "commissions for the year! " + sP.salesPerson2 + "'s total pay for the "
                        + "year will be $" + aPC.total6()); //outputs employees commission and pay if sales meet incentive
            }
            else if (aPC.totalSales2 >= 140000) {
                System.out.println(sP.salesPerson2 + " has earned $" + aPC.total7() + " in "
                        + "commissions for the year! " + sP.salesPerson2 + "'s total pay for the "
                        + "year will be $" + aPC.total8()); //outputs employees commission and pay if sales exceed targetSales
            }
            else if (aPC.totalSales2 < 112000) {
                System.out.println(sP.salesPerson2 + " will receive a total pay of $" + 
                        aPC.fixedSalary + " for the year. " + sP.salesPerson2 + " did not meet "
                        + "the sales incentive to earn commission for the year."); /*outputs employees end of year pay as fixed 
    salary since the sales amount is less than 80% of the sales target*/        
            }
//This is where I am trying to print the array created in the SalesPerson class
        System.out.println("");
        System.out.println("Here are both employee's sales in comparison:");
        System.out.println(sP.salesPerson1 + "\t" + sP.salesPerson2);
        System.out.print(n);      
        } 
     }
然后,我创建了这个SalesPerson类,其中包含我的数组:

package wk2individual;

public class SalesPerson {    
    String salesPerson1, salesPerson2;

public static void main(String[] args) {
    AnnualPayCalculator aPC = new AnnualPayCalculator();
        Double[][] sales = new Double[2][2];

        sales[0][0] = aPC.totalSales1;
        sales[0][1] = aPC.totalSales2;
        sales[1][0] = aPC.employee1TotalPay;
        sales[1][1] = aPC.employee2TotalPay;

     printArray(sales);   
    };

private static void printArray(Double[][] numbers){
    for (Double[] n : numbers){
       System.out.print(n);  
    }    
}

在第一个类中,我能够打印AnnualPayCalculator类中定义的计算总数。如何在第一个类中打印数组

您可能不需要2个
main
方法。在
Wk2Individual
中创建
sales
对象时,不会声明2d数组
sales
,因为静态方法和变量不是类的实例/对象的一部分。因此,您可能需要在
salesson
中创建一个非静态方法,如下所示

public class SalesPerson {    
    String salesPerson1, salesPerson2;

    public void createSales(AnnualPayCalculator aPC) {
        // you don't need to create aPC
        // AnnualPayCalculator aPC = new AnnualPayCalculator();

        Double[][] sales = new Double[2][2];

        sales[0][0] = aPC.totalSales1;
        sales[0][1] = aPC.totalSales2;
        sales[1][0] = aPC.employee1TotalPay;
        sales[1][1] = aPC.employee2TotalPay;
        printArray(sales);   
    }
}
此外,您可能正在尝试使用
Wk2Individual
类中的
aPC
对象中的值。但是您正在创建对象的新实例。因此,您应该像这样从
Wk2Individual
类传递旧的
aPC
对象:

System.out.println("");
System.out.println("Here are both employee's sales in comparison:");
System.out.println(sP.salesPerson1 + "\t" + sP.salesPerson2);
sP.createSales(aPC);
这将把包含所有计算值的aPC对象发送到
SalesPerson
类的
createSales()
,在那里将创建2d数组

现在你需要打印这个。为此,请在
SalesPerson
类中创建
print
方法:

private void printArray(Double[][] numbers){
    for (Double[] n : numbers){
        System.out.print(n);  
    }    
}
但不能打印这样的数组。这样做:

System.out.println(Arrays.toString(n));
AnnualPayCalculator
类中,您有几种使用全局变量的方法:
employee1TotalPay
employee2TotalPay
。例如,方法
total2()
。在这些方法中,您正在创建另一个同名变量。在
total2()。这意味着如果在该方法中的任何位置使用
employee1TotalPay
,它将使用本地
employee1TotalPay
变量(您在该方法中创建的变量)。要使用全局变量,请删除局部变量的声明:

employee1TotalPay = total1() + fixedSalary;
或者使用
this
关键字访问全局变量:

this.employee1TotalPay = total1() + fixedSalary;

我不知道你在做什么。看起来所有代码都在一个类中;另一个在哪里?打印数组的方法是私有的,因此外部类不能直接调用它。不过我不会这么做的。我不认为打印阵列是销售人员的责任。我也不建议在类实际上是复数时将其命名为单数,也不建议将其命名为变量,如
totalSalesN
。首先,我不认为这是一个子类。它只是一个名为
SalesPerson
类,具有入口点
main
。现在什么是父类
年度工资计算器
?这也是另一个对象/类,您希望在其中调用
printArray()
。对吗?因此,我更新了这篇文章,并提供了到目前为止的所有内容。希望这能澄清一些事情。谢谢你这么快回答!我看不到这些类之间有任何层次关系。这很有效!现在,数组在第二行为每个employeeTotalPay打印一个值0。我认为这是因为在aPC类中,共有2个总计为employeeTotalPay(共有2个总计为employee1TotalPay,共有2个总计为employee2TotalPay)。我需要程序获取正确的结果并将其放入数组。@Megan它显示为0,因为您尚未更新
employeeTotalPay
(1和2,两者)的值。这些值甚至还没有初始化。因此,
double
的默认值为0。我正在编辑我的答案以包括你做错了什么非常感谢!!现在看到它,我有点生气,我已经试图弄清楚这一个星期哈哈!你提供的一切正是我所需要的,我对你的帮助感激不尽!你真是个救命恩人!!
this.employee1TotalPay = total1() + fixedSalary;