How to read keyboard data to an array in Java?

Asked

Viewed 60 times

0

I’m trying to pass the data inside the matrix, but it’s not running the lines.

int i=0;
        int j=0;
        
        for(;i<3;i++){
            for(;j<4;j++){
                System.out.println("Valor para a posição ["+i+","+j+"]");
                notasAlunos[i][j]=entrada.nextDouble();
            }
        }

1 answer

0


To the point:

This occurs because, in the first iteration, when leaving the 2nd loop is ( for(;j<4;j++){ ) the variable j has value 4, and this value is not restarted, therefore in the second iteration j=4thus not satisfying the condition and therefore not entering the.

To correct assign value 0 to the variable j with each new iteration:

for(j=0;j<4;j++){
                System.out.println("Valor para a posição ["+i+","+j+"]");
                notasAlunos[i][j]=entrada.nextDouble();
            }

Browser other questions tagged

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