学校作业多维数组故障

时间:2016-08-20 23:54:14

标签: java arrays multidimensional-array methods call

我目前正在为我的Java编程类工作。我好像有点陷入困境。任何帮助我意识到自己做错的任何帮助都将不胜感激。

分配

编写执行以下操作的程序:

将下面的数据放入多维数组

向用户提示他们希望获得员工薪资统计数据的公司。

编写一个方法,将平均员工薪水返回为double。将公司编号和员工工资传递给此方法。

编写一个方法,将员工总薪资作为int返回。将公司编号和员工工资传递给此方法。

编写一个方法,将员工数作为int返回。将公司编号和员工工资传递给此方法。

在main方法中调用其他方法并输出结果。

请记住,我仍然是新手,并且正在努力理解编程的一些原则。

当我运行程序时,我得到的是位置而不是方法计算(输出错误):

bad output

这是我到目前为止所做的:

package salaries;

import java.util.Scanner;

public class Salaries {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        //declare, instantiate, and define value of multi array [3] [12]
        double [][] mSalary = { { 49920, 50831, 39430, 54697, 41751, 36110, 
                              41928, 48460, 39714, 49271, 51713, 38903},
                            { 45519, 47373, 36824, 51229, 36966, 40332,
                              53294, 44907, 36050, 51574, 39758, 53847},
                            { 54619, 48339, 44260, 44390, 39732, 44073,
                              53308, 35459, 52448, 38364, 39990, 47373}};

        //declare, instantiate, and define value
        //of single array for company names
        //and output values to user for selection
        String [] company = { "Alhermit", "Logway", "Felter" };
        for( int i = 0; i < company.length; i++ )
            System.out.println( "Company " + i + " : " +company[i] );

        Scanner scan = new Scanner( System.in );
        int cCompany;
        do{
            //ouput for user to select a company
            System.out.print("Select company: (0)" +company[0]+ ", (1)"
                            +company[1]+ "; (2)" +company[2]+ " > ");
            //scan user input into cCompany
            cCompany = scan.nextInt();


            //call number method
            num nums = new num();
            nums.number(mSalary, cCompany);

            //call total method
            total sum = new total();
            sum.total(mSalary, cCompany);

            //call average method
            avg cAvg = new avg();
            cAvg.average(mSalary, cCompany);


            //output statistics to user on selected company
            System.out.println( "You have selected the company " + company[cCompany] + ". " );
            System.out.println( company[cCompany] + " has " + nums + " of employees." );
            System.out.println( "A total employee salary of " + sum + "." );
            System.out.println( "The average employee salary is " + cAvg );
        }
            while( cCompany < 0 || cCompany > 2);
    }
}

//total class to calculate
//salary of user selected company
class total {

    public static int total( double [][] mSalary, int cCompany ){

        //assign variables
        int sum = 0;

        //for loop to calculate salary total of user input company
        for( int j = 0; j < mSalary[cCompany].length; j++ ){
            sum += mSalary[cCompany][j];

        }

    //return statement
    return sum;
    }
}

//average class to calculate
//average of user selected company
class avg {

    public static double average( double [][] mSalary, int cCompany){

        //assign variables
        int cAvg = 0;
        int sum = 0;
        int count = 0;

        //totals the values for the selected company by
        //iterating through the array with count.
        while( count < mSalary[cCompany].length){
            sum += mSalary[cCompany][count];
            count +=1;
        }

            cAvg = sum / mSalary[cCompany].length;
            return cAvg;
    }
}
//number class to calculate amount of
//employees in user selected company
class num {

    public static int number( double [][] mSalary, int cCompany){

        //assign variables
        int nums = 0;

        //number of employees based on length of colomn
        nums = mSalary[cCompany].length;
        return nums;
    }
}

1 个答案:

答案 0 :(得分:1)

([^; ]+|;) numssum都是您拥有的类的实例,您打印出这些类的实例。

(顺便说一下 - 你应该改变这些类的名称。类以大写字母开头。它将它们与变量区分开来。)

这有两个问题。

  1. 您正在实例化一个包含无数据并且没有cAvg方法的类。
  2. 您正在实例化一个只有静态方法来返回数据的类。您无需在 all 中实例化该类;相反,只需打印方法调用的结果。
  3. 这会将这些调用中的至少一个更改为:

    toString

    我把剩下的作为练习留给读者。