Input values into the matrix

Asked

Viewed 787 times

0

I have the following code that reads a 10x10 matrix:

public class t151 {

    static final int n = 10;

    public static void main (String[] args){

        int A[][] = new int [n][n];

        int i,j;

        for (i=0; i < n; i++){
            for (j=0; j < n; j++){
                System.out.print (A[i][j]); 
            }    
        }
    }
}

How do I manually insert the 100 elements of this matrix?

  • 1

    Can you explain better what you intend to do? At least for me it was not very clear.

  • Actually an array of integers(primitive) is started with all positions at 0. And in the other question you said you wanted to know the cause of the error ArrayIndexOfBoundExceptiononly. To work with each matrix position, simply change the line of the System.out.println with whatever each position receives. Gives an edited in the text, explaining better what you want to do so facilitates to elaborate a response.

  • from what I understand the code is only generating the positions of the matrix, so much so that its output by Meno here is being: 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000 I would like to know how I would manually put the values of each position (of each element) as user input (typing each element) or how I would adjust in the code a random sequence, for example, that measures an output like this: 1234567 etc.

  • I can do this for vectors but I can’t for matrix

  • See my previous comment(if it does not appear full, update this page).

  • edited the question.

Show 1 more comment

1 answer

1


If the intention is to manually populate the matrix, as said in the comments, just change the line within the inner is, making the matrix receive the desired data. The for external control the change of matrix lines, and the for internal controls the change of columns in each row.

In the code below, it would be necessary to enter the 100 positions via user input:

import java.util.Scanner;

public class t151 {

    static final int n = 10;

    public static void main (String[] args){

        Scanner sc = new Scanner(System.in);

        int A[][] = new int [n][n];

        int i,j;

        //este laço controla a mudança de linhas da matriz
        for (i=0; i < n; i++){
            //já este laço controla
            // a mudança de colunas em cada linha da matriz
            for (j=0; j < n; j++){
                A[i][j] = sc.nextInt(); 
            }    
        }
    }
}

If to fill in with some other value, which does not come from a user input, just remove the sc.nextInt() and put the data that will be stored.

See the example working on IDEONE.

  • as in the vectors the input stays inside the loop of the single line my doubt was of how it would be made in the matrix, if it would be in two parts (first inside the loop of the rows and after the columns). that was the doubt

  • @Leko edited the answer explaining better how this kind of loop works, see if you can understand clearly, anything, just comment :)

  • thank you very much

  • @Leko you know you can vote for everything on the site? You can vote on all answers given in your questions (not only the one you accepted, you can vote on other questions, on answers to other things than yours that you think are cool. It would be good to review at least your questions and see if the answers given in them deserve a vote. If you don’t know the difference between accepting a reply and voting, see the [tour].

Browser other questions tagged

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