Z-shaped square with array

0

I'm trying to make a java algorithm to display a string of letters arranged in a "Z" format. If the alphabet finishes, go back to A and continue until you finish the matrix. I'm really lost in relation to the matrix. Could someone help?

An example of what I need to do:

packageLearning;importjava.util.Scanner;publicclassZ{publicstaticvoidmain(String[]args){Scannerscan=newScanner(System.in);System.out.println("Enter The Number Of Matrix Rows ");
        int matrixRow = scan.nextInt();
        System.out.println("Enter The Number Of Matrix Columns: ");
        int matrixCol = scan.nextInt();



        if (matrixRow > 50 && matrixCol > 50) {
            System.out.print("ERROR");
        }
        if (matrixRow != matrixCol) {
            System.out.print("ERROR");
        if (matrixRow <= 2 && matrixCol <= 2) {
            System.out.print(" ERROR");
        }


        int[][] matrix = new int[matrixRow][matrixCol];

    for (int i = 65; i < matrixRow; i++) {
        for (int j = matrixRow; j < matrixCol; j++) {

        }

        for (int l = 0; l < matrixRow; l++) {
            for (int c = 0; c < matrixCol; c++) {
                System.out.print(array[matrixRow][matrixCol] + "\t");
            }

            System.out.println();

        }

    }
}

}

    
asked by anonymous 10.04.2018 / 02:34

1 answer

0

Hello, Juliosk!

To begin with, I suggest you put some sort of code termination or validation so that the algorithm does not create the array when it does not have the same number of rows and columns, for example:

if (matrixRow != matrixCol) System.out.print("ERROR");

You can flag here with a boolean variable (initializing it before in your code) and it would look like this:

if (matrixRow != matrixCol) { System.out.print("ERROR: número de linhas e colunas da matriz não é igual!"); criarMatriz = false; }

So, before you start looping back to 'draw' the array you check whether criarMatriz is as true . This way, it is clearer to those who use the algorithm if the array could even be drawn and, if not, what was the error found. (You can do this with the other checks too!)

For the printing of the letters in the array, you could use an array of chars alfabeto containing all the letters of the same; when it arrived in the 'Z', the for would take the index to the beginning, solving the problem to finish the alphabet before the matrix.

Example: char[] alfabeto = new char[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z];

For impressions, I suggest that you make 3 different loops: one for the top horizontal line, one for the diagonal line and one for the bottom horizontal line.

See how for can be:

if(criarMatriz == true){ //verifica se pode desenhar a matriz int a = 0; //índice do alfabeto para começar no 'A' for(int i = 0; i < matrixCol; i++){ if(a == 26) //verifica fim do alfabeto a = 0; System.out.print(alfabeto[a] + " ") }

The same situation can be applied to the other two loops, and in the diagonal the for would start at the end of the columns and rows and would return to the beginning, and then finish with the last loop.

Remember to take care when using System.out.print () and System.out.println () to get everything in its place!

    
20.04.2018 / 02:55