+ 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*/
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);
}
}
+ 3
You're welcome, glad if it helps : )
+ 1
sorry, It's x<heights.length
+ 1
Thanks. That was very helpful 👏