Because the System.out.print.ln of this if is not played

Asked

Viewed 135 times

0

Can someone tell me why my code doesn’t run the System.out.println(""); end if the if 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(""); 
            }
        }
  • 4

    How do you know it doesn’t perform? It’s empty.

  • Yes but not supposed to run println , ie make a paragraph, ?

  • What is the purpose of this code?

  • When he should make a new paragraph with that 'Sout'?

  • diegofm this is a method for other arrays.

2 answers

3

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

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

1


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

The last column of the matrix will always be equal to the row size subtracting one and the condition is backwards

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.

  • Obg sorry for wasting time

Browser other questions tagged

You are not signed in. Login or sign up in order to post.