到目前为止,下面的代码是我什么都没有的。我打算声明一个主要的7×10二维数组。将该数组传递给使用随机整数[1,9]填充数组的方法。将填充的数组传递给一种方法,该方法将打印二维数组,并在每一行的末尾显示该行的总和,在每一列的末尾显示该列的总和。行和列标签也必须打印。任何帮助将不胜感激。

package twodimensionalarray;

import java.util.Arrays;
import java.util.Random;

/**
 *
 * @author Kristy7
 */
public class TwoDimensionalArray {

    //This method is for generating random integers between 1 and 9.
    public static int randInt(){
        Random rand = new Random();
        int randNum;
        randNum = rand.nextInt(9)+1;
        return randNum;

    }

    //This method is for filling a 2D array with random integers
    public static int[][] fillArray(int[][] toFill){
        int[][] toReturn = new int[0][0];

        for(int r = 0; r < toFill.length; r++){
            for(int c = 0; c < toFill[0].length; c++){
                toReturn[toFill[r]][toFill[c]] = randInt(toFill);
            }

        }

        return toReturn;
    }

    //This method is for for summing rows and columns, and printing rows, columns, the sum of ruws at each row, and the sum of columns at each column.
    public static void summingRC(int[][] toSum){
        int[] colSums = new int[0];
        for(int i = 0; i < toSum.length; i++){
            System.out.print(i);
        }
        System.out.println();
        int sum = 0;
    for (int r = 0; r < toSum.length; r++) {

        for (int c = 0; c < toSum[r].length; c++) {
            sum += toSum[c][r];
            colSums[c] += toSum[r][c];
        }

        System.out.println(sum);
    }
    System.out.println(colSums);


    }

    /**
     * @param args the command line arguments
     *
     */

    public static void main(String[] args) {

        //Declare a 7x10 2D array
        int [][] twoDArray = new int[7][10];

        //call fillArray method.
        int[][] fillingArray = fillArray(twoDArray);

        //call SummingRC method
        summingRC(fillingArray);

    }

}

最佳答案

您可能已经知道,此代码存在很多问题。它们中的大多数源自错误地实例化和访问数组。我将其修复:

public static int randInt(){
    Random rand = new Random();
    int randNum;
    randNum = rand.nextInt(9)+1;
    return randNum;

}

//This method is for filling a 2D array with random integers
public static int[][] fillArray(int[][] toFill){
    int[][] toReturn = new int[toFill.length][toFill[0].length];

    for(int r = 0; r < toFill.length; r++){
        for(int c = 0; c < toFill[0].length; c++){
            toReturn[r][c] = randInt();
        }
    }
    return toReturn;
}

//This method is for for summing rows and columns, and printing rows, columns, the sum of ruws at each row, and the sum of columns at each column.
public static void summingRC(int[][] toSum){
    int[] colSum = new int[toSum[0].length];
    int[] rowSum = new int[toSum.length];
    for(int i = 0; i < toSum.length; i++){
        for(int j = 0; j < toSum[i].length; j++){
            colSum[j] += toSum[i][j];
            rowSum[i] += toSum[i][j];
        }
    }
    System.out.println("Matrix: ");
    print2dArray(toSum);
    System.out.println("Col sum:");
    printArray(colSum);
    System.out.println("Row sum:");
    printArray(rowSum);

}

/**
 * @param args the command line arguments
 *
 */

public static void main(String[] args) {

    //Declare a 7x10 2D array
    int [][] twoDArray = new int[7][10];

    //call fillArray method.
    int[][] fillingArray = fillArray(twoDArray);

    //call SummingRC method
    summingRC(fillingArray);

}

public static void print2dArray(int[][] arr){
    for(int i = 0; i < arr.length; i++){
        for(int j = 0; j < arr[i].length; j++){
            System.out.print(arr[i][j] +  " ");
        }
        System.out.println();
    }
}

public static void printArray(int arr[]){
    for(int i = 0; i < arr.length; i++){
        System.out.print(arr[i] +  " ");
    }
    System.out.println();
}


在很多地方都可以实例化一个长度为0的数组。在那个时候这基本上只是一个无用的数组,因为您不能在其中添加任何值。例如:

int[] colSums = new int[0];

09-05 01:57
查看更多