I am starting, and here is the method I struggle with.
Write a method called percentEven that takes an array of integers as a parameter and returns the percentage of even numbers in the array as a real number. For example, if the array stores the elements {6, 2, 9, 11, 3}, then your method should return 40.0. If the array does not contain even elements or elements at all, return 0.0.
that's what i still have ...
public static double percentEven(int[]a){
int count = 0;
double percent = 0.0;
if (a.length > 0){
for ( int i = 0; i < a.length; i++){
if ( a[i] % 2 == 0){
count++;
}
}
percent =(count/a.length)*100.0;
}
return percent;
}
I keep returning 0.0 when the array contains a combination of even and odd elements, but works fine for all even arrays of elements or the entire odd array? I do not see where the problem is? thanks in advance.
source
share