java - 学校作业多维数组麻烦

标签 java arrays multidimensional-array methods call

我目前正在为我的 Java 编程类(class)做作业。我似乎让自己陷入了困境。任何帮助我认识到我做错了什么的帮助将不胜感激。

作业

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

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

提示用户他们想要查看员工薪资统计信息的公司。

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

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

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

在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;
    }
}

最佳答案

numssumcAvg 都是您拥有的类的实例,您将打印出这些类的实例。

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

这有两个问题。

  1. 您正在实例化一个不包含数据并且没有toString方法的类。
  2. 您正在实例化一个类,该类仅具有用于返回数据的静态方法。您不需要完全实例化该类;相反,只需打印方法调用的结果。

这将至少将其中一个调用更改为:

System.out.println( company[cCompany] + " has " + num.number(mSalary, cCompany); + " of employees." );

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

关于java - 学校作业多维数组麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39059699/

相关文章:

java - 如何将复选框添加到表中以读取和写入它在 JavaFX 中表示的对象属性

java - 使用公网IP远程访问MySQL数据库出错

javascript - 使用大数组进行反向地理编码是最快的方法吗? - JavaScript 和性能

javascript - 通过 JS 中所需的组大小将一维数组转换为多维数组的更好方法

numpy - 我如何获得或创建与rust的ndarray crate 等效的numpy的amax函数?

java - 如果这两个数组在同一位置具有相同的数字,如何显示?

java - 如何在 Tomcat 上运行的 Spring REST 应用程序中查找未使用的端点?

jQuery:检查列是否包含特定值

java - 有没有办法在这段代码中避免@SuppressWarnings?

php - 如何对多维数组求和