-1
package teeeeeee;
public class Hah {
    public static void main(String[] args) {
                    int  x = 0;
                while (x == 0){
                    int[][] array = {{0,0,0},{0,0,0},{0,0,0}};
                array = fillMatrix();


                if (sumRows(array)&& sumColumns(array)&& sumDiagonals(array)){
                System.out.println("you have a magic square");
                x = 1;

            }
                else
                    System.out.println("not a magic square");

                }
            }

            public static int[][] fillMatrix(){
                 int[][] array = new int [] []
                {
                        { 2 , 7 , 6 },
                        { 9 , 5 , 1 },
                        { 4 , 3 , 8 }
                };
                for (int i = 0; i < array.length; ++i){
                    System.out.println(array[i]);
                }
                return array;
            }

            public static boolean sumRows(int [][] array){
                for (int r = 0; r < 3; r++){
                    int sum = 0;
                    for (int c = 0; c < 3; c++)
                        sum += array[r][c];
                    if (sum != 15)
                        return false;
            }
            return true;
            }

            public static boolean sumColumns(int [][] array){
                for (int c = 0; c < 3; c++){
                    int sum = 0;
                    for (int r = 0; r < 3; r++)
                        sum += array[r][c];
                    if (sum != 15)
                        return false;
            }
                return true;
            }
            public static boolean sumDiagonals(int [][] array){

                if (array[0][0] + array[1][1] + array[2][2] != 15)
                    return false;

                if (array[0][2] + array[1][1] + array[2][0] != 15)
                    return false;

                return true;
            }
    }

how do i print out this array without it being random numbers and letters i have tried many ways but every time it just come out in the randomness and not the array i want it to print any help would be nice. the array i want to rint is in the method fill matrix.

TimeToCode
  • 901
  • 2
  • 16
  • 34
jnever
  • 1

2 Answers2

0

It is not printing random numbers and letter, it prints the default array toString() representation. To print the contents of the 2D array, you might want to try this:

public static int[][] fillMatrix(  ) {
    int[][] array = new int [] [] { { 2 , 7 , 6 }, { 9 , 5 , 1 }, { 4 , 3 , 8 } };

    for (int i = 0; i < array.length; ++i) {
         System.out.println(Arrays.toString(array[i]));
    }

    return array;
}
0

You can go for a nested loop

    for(int i = 0; i < array.length; i++)
    {
           for(int j = 0; j< array[i].length; j++)
           {
                 System.out.print(array[i][j]);
           }

           System.out.println();
    }

Hope this helps you!

Kushal Jogi
  • 265
  • 5
  • 15