Show Figure "X" in a matrix, JAVA

1

I have to make an X-shaped matrix using only numbers 9 and 1 where the number 1 is the one that has to form the X. The user chooses the size of the matrix, I have a bit of the script where I have problems in the for loops and when I run the program throws the 9's and 1's incomplete and the matrix is not well generated apart from that I can not achieve an X.

System.out.println("De que tamaño quiere el arreglo?.");
int cantidad = entrada.nextInt();

int[][]arreglo = new int[cantidad][cantidad];

for ( int i=0; i < arreglo.length ;i++){


    for (int j=0;  j<arreglo.length;j++){

        if((i==j)  ||  (i + j ==arreglo.length-1)){

            arreglo[i][j] = 1;
            System.out.print(arreglo[i][j] + "\t");
            System.out.println();

        }
        else{
            arreglo[i][j] = 9;
            System.out.print(arreglo[i][j] + "\t");
            System.out.println();
    
asked by Diego Magallon 23.02.2017 в 18:42
source

2 answers

2

Try this ...

    System.out.println("De que tamaño quiere el arreglo?.");
    int cantidad = entrada.nextInt();

    int[][]arreglo = new int[cantidad][cantidad];

    for ( int i=0; i < arreglo.length ;i++){
          for (int j=0;  j<arreglo.length;j++){
                int x = i + 1;
                if((i==j)  ||  (j == (cantidad - x))){
                    arreglo[i][j] = 1;
                    System.out.print(arreglo[i][j] + " ");
                }
                else{
                   arreglo[i][j] = 9;
                   System.out.print(arreglo[i][j] + " ");
                }
          }  
           System.out.println();
    }   
    
answered by 23.02.2017 / 23:57
source
-1

Your code is fine, the detail is that you print a line break after showing the result of the position of the matrix

    
answered by 23.02.2017 в 23:09