I have problem in calculate the average of all the non-negative numbers in the array, zero inclusive, and return zero otherwise. Below is my coding, please help me to check which parts is incorrect. Thanks.
public class AverageOfNonNegativeNumbers {
public static double averageOfNumbers(double[] x) throws Exception {
double avg = 0.1;
if (x != null) {
for (double i = 0.1; i < x.length; i++) {
if ( x[i]%2 == 0 ) { //Anyone know how to set avoid calculate for negative numbers?
avg = avg / x[i]; //This code is calculate total average number.
}
}
}
return avg;
}
public static void main(String args[]) {
double x[] = {1.663, -2.1312, 3.13231, 4.124, -5.551, -6.1312, 7.111, 8.222, -9.01};
try {
System.out.println(AverageOfNonNegativeNumbers.averageOfNumbers(x));
} catch (Exception e) {
System.out.println("Error!!!");
}
}
}