Java中的2D阵列打印格式

时间:2014-12-03 21:18:14

标签: java arrays

我有一个分配,它要求用户输入2D数组的大小,然后询问数组中的数字,然后进行一些计算(如sum& max)。 我遇到的问题是当它向用户询问数组中的数字时,我无法将其格式化为它应该用于赋值的方式。同时也难以以格式显示最终数组。

用户输入示例:

输入[0] [0]:

输入[0] [1]:

最终数组显示示例:

[1,2,3]

[4,5,6]

这是我的代码到目前为止,我已经评论了我需要帮助的部分。

import java.util.Arrays;
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++) {
                /*Need to format it like this: Enter [0][0]:
                 Enter [0][1]:
                 etc. until it forms the 2D Array but not sure how to get
                 it to print out like that.
                 */
                System.out.print("      Enter " + ret + ": ");
                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 rows = anArray.length;
        int cols = anArray[0].length;

        int[] sum = new int[rows];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                sum[i] += anArray[i][j];
            }
        }

        for (int x = 0; x < anArray.length; x++) {
            System.out.println("Sum of row " + x + " = " + sum[x]);
        }
    }

    public static void columnSum(int[][] anArray) {
        int rows = anArray.length;
        int cols = anArray[0].length;

        int[] sum = new int[cols];
        for (int i = 0; i < cols; i++) {
            for (int j = 0; j < rows; j++) {
                sum[i] += anArray[j][i];
            }
        }

        for (int x = 0; x < anArray.length || x > anArray.length; x++) {
            System.out.println("Sum of columnn " + x + " = " + sum[x]);
        }
    }

    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]);

                /*When displaying the whole array before the end of the program
                 it needs to be formatted like this:
                 [1,2,3]
                 [4,5,6]
                 [7,8,9]
                 but I'm not sure how to get it to print like that.
                 */
                System.out.print(", ");
            }
            System.out.println();
        }
    }
}
class Arrays2DDemo {

    public static void main(String[] args) {

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

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

        Array2DMethods.max(anArray);
        System.out.println();
        Array2DMethods.rowSum(anArray);
        System.out.println();
        Array2DMethods.columnSum(anArray);

        Array2DMethods.isSquare(anArray);
        System.out.println("\nIs this a square array? " + Array2DMethods.isSquare(anArray));

        System.out.println();
        Array2DMethods.displayOutputs(anArray);
    }
}

感谢您提供任何帮助。

2 个答案:

答案 0 :(得分:0)

输入部分:

System.out.print(String.format("      Enter [%d][%d]: ", i, j));
ret[i][j] = keyboard.nextInt();

输出部分:

for (int[] row : anArray) {
    System.out.println(Arrays.toString(row));
}

答案 1 :(得分:0)

尝试此输入部分:

System.out.print("      Enter ["+ i +"]["+ j +"] : ");

输出:

for(int i=0; i<anArray.length; i++){
    System.out.println("[");
        for(int j=0; j<anArray[i].length; j++){
            System.out.print(anArray[i][j]+" ");
            System.out.print(",");
        }
        System.out.print("\b");
        System.out.print("]");
    }