To calculate average in Java, first sum the numbers in a
for
loop, then divide by amount of numbers.Here's how you do it:
double[] numbers = new double[] { 1.0, 5.0, 12.6 };
double sum = 0.0;
for (int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
double average = sum / numbers.length;
// 6.2
System.out.println(average);