-2

Can you please help I am new to proggraming and I don not even no what this error is caused from here is my code. I am trying to make a working Resistence formula but its not working.

 int n=kb.nextInt();
        double massiv[]=new double[n];
        for(int i=0;i<=massiv.length;i++){
            massiv[i]=kb.nextDouble();
        }
        for(int i=0;i<=massiv.length;i++){
            gr=massiv[i]*gr;
            dr=massiv[i]+dr;
        }
        Re=gr/dr;
        System.out.println(+Re);
Simon Cankov
  • 51
  • 1
  • 7
  • 4
    Change `<=` to `<`. `Array`'s are zero indexed based, so the last index will be size -1, so you need to loop while less than the length – GBlodgett Nov 23 '18 at 22:10
  • What is `System.out.println(+Re);` supposed to do? – GBlodgett Nov 23 '18 at 22:11
  • Indexes in an array go from 0 to n-1 where n is the length of the array ... you are also accessing the nth index by using <= in your for loops ... so if you change it to <, you won't get the exception – mettleap Nov 23 '18 at 22:12

1 Answers1

3

Arrays are 0-indexed. An array of size 2 has 2 indexes: 0 and 1. Your loops are trying to access index 2 (since they're using <=), which doesn't exist.

Replace <= in your for-loops with <

Krease
  • 15,805
  • 8
  • 54
  • 86