Because the System.out.print.ln of this if does not play

0

Can someone tell me why my code does not execute the System.out.println(""); of the end if if it returns true .

public static void tabelear (int tabela[][]){


            for(int linha=0;linha < tabela.length; linha++){
                for(int coluna=0; coluna < tabela[linha].length ; coluna++ ){
            System.out.print(tabela[linha][coluna]+"   ");
                if (coluna-1 == tabela[linha].length){
                System.out.println(""); 
            }
        }
    
asked by anonymous 08.06.2017 / 20:25

2 answers

1

The line break is not being printed because the condition is wrong.

The last column of the array will always be equal to the size of the line subtracting one and the condition is reversed

public static void tabelear (int tabela[][]){
    for(int linha=0;linha < tabela.length; linha++) {
        for(int coluna=0; coluna < tabela[linha].length ; coluna++ ){
            System.out.print(tabela[linha][coluna] + "   ");

            if (coluna == tabela[linha].length - 1){
                System.out.println(""); 
            }
        }
    }
}

This can be easily simplified to

public static void tabelear2 (int tabela[][]){
    for(int linha=0;linha < tabela.length; linha++) {
        for(int coluna=0; coluna < tabela[linha].length ; coluna++ ){
            System.out.print(tabela[linha][coluna] + "   ");
        }

        System.out.println(""); 
    }
}

See working on repl.it.

    
08.06.2017 / 20:33
3

If your idea is to make a new paragraph at each end of the line, you could do this:

for (int linha = 0; linha < tabela.length; linha++) {
    for (int coluna = 0; coluna < tabela[linha].length; coluna++) {
        System.out.print(tabela[linha][coluna] + "   ");
     }
     System.out.println("");
}
    
08.06.2017 / 20:33