创建一个二维数组操作,该二维数组操作位于另一个单独的类中?

时间:2019-03-02 02:01:44

标签: java arrays multidimensional-array

因此,当我尝试创建从一个类到另一个类的调用方法时,发现空白终端出现错误。我不为所动,现在已经为之努力了几天,我该怎么办/我想念什么?

ArrayOperations2D:

public class ArrayOperations2D {
    public static double getTotal(double[][] array) {
        double total = 0;
        for (int row = 0; row < array.length; row++) {
            for (int col = 0; col < array[row].length; col++) {
                total += array[row][col];
            }
        }
        return total;
    }

    public static double getAverage(double[][] array) {
        return getTotal(array) / getElementCount(array);
    }

    public static double getRowTotal(double[][] array, int row) {
        double total = 0;
        for (int col = 0; col < array[row].length; col++) {
            total += array[row][col];
        }
        return total;
    }

    public static double getColumnTotal(double[][] array, int col) {
        double total = 0;

        for (int row = 0; row < array.length; row++) {
            total += array[row][col];
        }

        return total;
    }

    public static double getHighestInRow(double[][] array, int row) {
        double highest = array[row][0];

        for (int col = 1; col < array[row].length; col++) {
            if (array[row][col] > highest) {
                highest = array[row][col];
            }
        }
        return highest;
    }

    public static double getLowestInRow(double[][] array, int row) {
        double lowest = array[row][0];
            //first argument
        for (int col = 1; col < array[row].length; col++) {
            // secondary argument
            if (array[row][col] < lowest) {
                lowest = array[row][col];
            }
        }
        return lowest;
    }

    public static int getElementCount(double[][] array) {
        int count = 0;

        for (int row = 0; row < array.length; row++) {
            count += array[row].length;
        }
        return count;
    }

    public static void main(String[] args) {

        double[][] studentGrades = { { 75.5, 65.14, 43.94, 91.9 },
                { 23.7, 20.22, 55.69, 10.10 } };


        System.out.println("Total : " + getTotal(studentGrades));
        System.out.println("Average : " + getAverage(studentGrades));

        System.out.println("Total of row 0 : "
                + getRowTotal(studentGrades, 0));
        System.out.println("Total of row 1 : "
                + getRowTotal(studentGrades, 1));

        System.out.println("Total of column 0 : "
                + getColumnTotal(studentGrades, 0));
        System.out.println("Total of column 1 : "
                + getColumnTotal(studentGrades, 2));

        System.out.println("Highest in row 0 : "
                + getHighestInRow(studentGrades, 0));
        System.out.println("Highest in row 1 : "
                + getHighestInRow(studentGrades, 1));

        System.out.println("Lowest in row 0 : "
                + getLowestInRow(studentGrades, 0));
        System.out.println("Lowest in row 1 : "
                + getLowestInRow(studentGrades, 1));

    }
}

因此,下一步是我需要创建一个完全独立的类,并开始创建一种方法来从上面调用相同的方法。当我开始创建它时,我想使用:

public class A3MAIN {

public static void main(String[] args) {
    double[][] studentGrades = { { 75.5, 65.14, 43.94, 91.9 },
            { 23.7, 20.22, 55.69, 10.10 } };

基本上,对于需要从ArrayOperations2D调用的每个项目重复相同的命令。

 ArrayOperations2D.getRowTotal(studentGrades, 0);
    ArrayOperations2D.getRowTotal(studentGrades, 1);

这有意义吗?因为这将火箭科学带给了我。

0 个答案:

没有答案