Input values in array

0

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

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 enter the 100 elements of this array?

    
asked by anonymous 04.11.2016 / 00:34

1 answer

1

If the intention is to manually fill the array, as I said in the comments, simply change the line inside the innermost for, causing the array to receive the desired data. The for outermost controls the change of the rows of the array, and the for inner 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 it is to fill in some other value, it does not come from a user input, just remove sc.nextInt() and put the data that will be stored.

See the example working on IDEONE .

    
04.11.2016 / 01:00