Java problems for calling an array

1
Hello, could anyone tell me why the program is not finding the array I (I'll flag in the code below:

Error:

  

error: can not find symbol R + = I [P0]; ^ symbol: variable I location: class math 1 error

class Matematica {
    public static void main (String args[]) {

    Scanner ler = new Scanner(System.in);

    int T = ler.nextInt();

    for(int SN=0 ; SN < T ; SN++){
        int N = ler.nextInt();
        int I[]=new int [N];            
        I[SN]=ler.nextInt();
    }              

    int Q = ler.nextInt();

    for(int SQ=0 ; SQ < Q ; SQ++) {
        int P [] = new int[2];
        P[0] = ler.nextInt();
        P[1] = ler.nextInt();

        while(P[0] <= P[1]) {

            int P0=P[0];
            int R = 0;
            R+= I[P0]; // O erro está nessa linha
            if(P[0] == P[1])          
                System.out.printf("%d",R);

            P[0]++; 
        }
    }     
}    
    
asked by anonymous 10.12.2015 / 17:31

1 answer

3

This is because in this line you are creating the variable I within block for , like this:

for(int SN=0 ; SN < T ; SN++){
    int N = ler.nextInt();
    int I[]=new int [N];            
    I[SN]=ler.nextInt();
} 

In this way, the I variable will only exist within this block of code.

One way to solve would be to declare the variable above for

int I[] = new int[0];        
...
for(int SN=0;SN<T;SN++){
    ...
    I = new int [N]; 

Council

Always use descriptive names for your variables, this makes the code extremely simpler to understand. If you are declaring a variable to represent the name of a client, use String nomeCliente = "Joaquim"; . This way you (and anyone else) will understand what this variable does (practically) just look at its name.

    
10.12.2015 / 17:48