+ 1

Attempting a Sum Array

I am trying to recreate the array they told me to answer, but when I enter it all it gives me a error. Any ideas? package sample; public class Example { double sum = 0.0; int[] myArray = {10,20,25}; for(int x = 0; x < 4; x++){ sum+= myArray[x]; } System.out.println(sum); }

1st Nov 2018, 7:35 PM
Vell Jackson
Vell Jackson - avatar
1 Respuesta
+ 4
x only has 3 elements, so x should be < 3 instead of < 4 in the for loop.
1st Nov 2018, 7:37 PM
Andre Daniel
Andre Daniel - avatar