简体   繁体   中英

2D Array print format in Java

I have an assignment where it asks the user for input on the size of a 2D array, then asks for the numbers that goes in the array, and then makes some calculations (like sum & max). What I'm having trouble with is when it asks the user for the numbers that go in the array I can't get it to format the way it's supposed to be for the assignment. Also having trouble with displaying the final array in the format it should be in as well.

Example for user input:

Enter [0][0]:

Enter [0][1]:

Etc.

Example for final array display:

[1,2,3]

[4,5,6]

etc.

here's my code so far and I've commented the parts that I need help with.

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

Thanks for any help you can give me.

For the input part:

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

For the output part:

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

Try this input part:

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

For output :

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("]");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM