2D阵列方法&演示

时间:2014-12-01 15:56:20

标签: java arrays

我有一个设计和实现处理2D阵列的方法的任务。

它需要一个具有以下静态方法的实现类(Array2DMethods): readInputs()读取用户的行数和列数,然后读取该大小的相应条目。例如:如果用户为#行输入3,为#列输入3,则它将声明一个10的数组并读取9个条目。

max(int [][] anArray)它返回2D参数数组anArray

中的最大值

rowSum(int[][] anArray)它返回anArray

的第x行中元素的总和

columnSum(int[][] anArray)它返回anArray列x中元素的总和**小心w /不同长度的行

isSquare(int[][] anArray)检查数组是否为正方形(意味着每行与anArray本身的长度相同)

displayOutputs(int[][] anArray)显示2个Dim数组元素

它还需要一个测试方法的测试类(Arrays2DDemo)。

我已经评论了我遇到问题的部分。我不确定如何测试除readInputs方法之外的方法,也不确定如何格式化要求用户输入每行数字的部分。

到目前为止,这是我的代码:

import java.util.Scanner;

class Array2DMethods {

    public static int [][] readInputs(){
        Scanner keyboard = new Scanner(System.in);

        System.out.print("      How many rows? ");
        int rows = keyboard.nextInt();

        System.out.print("      How many columns? ");
        int columns = keyboard.nextInt();

        int [][] ret = new int[rows][columns];

        for (int i = 0; i<ret.length; i++) {
            for (int j = 0; j < ret[i].length; j++) {
                System.out.print("please enter an integer: "); //Need to format like Enter [0][0]: ... Enter [0][1]: ...etc. 
                ret[i][j] = keyboard.nextInt();
            }
        }

        return ret;
    }

    public static int max(int [][] anArray) {
        int ret = Integer.MIN_VALUE;

        for (int i = 0; i < anArray.length; i++) {
            for (int j = 0; j < anArray[i].length; j++) {
                if (anArray[i][j] > ret) {
                    ret = anArray[i][j];
                }
            }
        }

        return ret;
    }

    public static void rowSum(int[][]anArray) {
        int ret = 0;

        for (int i = 0; i<anArray.length; i++) {
            for (int j = 0; j < anArray[i].length; j++) {
                ret = ret + anArray[i][j];
            }
        }
    }

    public static void columnSum(int[][]anArray) {
        int ret = 0;

        for (int i = 0; i < anArray.length; i++) {
            for (int j = 0; j < anArray[i].length; j++) {
                ret = ret + anArray[i][j];
            }
        }
    }

    public static boolean isSquare(int[][]anArray) {

        for (int i = 0, l = anArray.length; i < l; i++) {
            if (anArray[i].length != l) {
                return false;
            }
        }
        return true;
    }

    public static void displayOutputs(int[][]anArray) {
        System.out.println("Here is your 2Dim Array:");
        for(int i=0; i<anArray.length; i++) {
           for(int j=0; j<anArray[i].length; j++) {
                System.out.print(anArray[i][j]);
                System.out.print(", ");
            }
            System.out.println();
        }
    }
}

Class Arrays2DDemo:

public class Arrays2DDemo {

    public static void main(String[] args){

        System.out.println("Let's create a 2Dim Array!");

        int [][] anArray = Array2DMethods.readInputs();

        Array2DMethods.max(anArray);

        Array2DMethods.rowSum(anArray);
        //need to print out and format like this: Ex Sum of row 1 = 60 ...etc
        Array2DMethods.columnSum(anArray);
        //need to print out and format like this: Ex Sum of column 1 = 60 ...etc.
        Array2DMethods.isSquare(anArray);
        //need to print out is this a square array? true
        Array2DMethods.displayOutputs(anArray);
        //need it to be formatted like [10, 20, 30] etc
    }
}

1 个答案:

答案 0 :(得分:2)

假设你想让anArray成为你在输入时读入的数组,你应该将该变量命名为......

public static void main(String[] args){

    System.out.println("Let's create a 2Dim Array!");

    int[][] anArray = Array2DMethods.readInputs();

    System.out.println("max " + Array2DMethods.max(anArray)); 

    Array2DMethods.rowSum(anArray);
    Array2DMethods.columnSum(anArray);

    System.out.println("Square " + Array2DMethods.isSquare(anArray));

    Array2DMethods.displayOutputs(anArray);

}

假设您有一个函数f,它只需要一个输入x。问题是你要求计算机评估f(x)而不告诉它x是什么。如果您给x一个值,例如x = 3,那么请f(x)变为合法,因为它变为f(3),可以对其进行评估。