I have the following Java method that is supposed to calculate the average of the array:
public class Kata{
public static double find_average(int[] array){
int avr = 0;
for(int a = 0; a < array.length; a++) {
avr = avr + array[a];
}
System.out.println("hello");
double average = avr / array.length;
return average;
}
}
I am confused because I get a stack trace error: expected:<2.5> but was:<2.0> when I use this method. Additionally, printing out "hello" prints 3 times. However, when I return just the number 3, "hello" only prints once. Why does System.out.println("hello") print out multiple times depending on the return value and how can I return the correct array average?
This is particularly confusing to me because one of the answers on this thread indicates that this approach should work for average. How to manipulate arrays. Find the average. Beginner Java