+ 1

Can't do addition of arrays with doubles?

public class howTall { public static void main(String[] args) { double [] heights = {1.50, 1.60, 1.63, 1.79}; double sum=0.0; for (double x=0.0; x<heights; x++) { sum+=heights[x]; } System.out.println(sum); } } /*Outputs error*/

30th Sep 2018, 11:18 PM
Strovosky
Strovosky - avatar
4 Respostas
+ 3
Use int for array index <x>, we can't refer to an array element using floating point type (double). public class howTall { public static void main(String[] args) { double [] heights = {1.50, 1.60, 1.63, 1.79}; double sum = 0; for (int x = 0; x < heights.length; x++) { sum += heights[x]; } System.out.println(sum); } }
1st Oct 2018, 1:08 AM
Ipang
+ 3
You're welcome, glad if it helps : )
1st Oct 2018, 1:42 AM
Ipang
+ 1
sorry, It's x<heights.length
30th Sep 2018, 11:24 PM
Strovosky
Strovosky - avatar
+ 1
Thanks. That was very helpful 👏
1st Oct 2018, 1:41 AM
Strovosky
Strovosky - avatar