dimanche 5 avril 2015

Else if statements

I'm a beginner to java... if statements followed by else if statements are evaluated in order until one that evaluates to true is found, and I've seen many examples of this. But in this program, both statements (the if and the else if) are evaluated. Why? Thanks!



public int centeredAverage(int[] nums) {

int[] nums = {1, 1, 5, 5, 10, 8, 7};

int sum = 0;
int centered = 0;
int min = nums[0];
int max = nums[0];
int i = 0;


for (i = 0; i < nums.length; i++){
if (nums[i] < min){
min = nums[i];
}
else
if (nums[i] > max){
max = nums[i];
}
sum += nums[i];
centered = ((sum-max-min)/(nums.length-2));

}
return centered;
}

Aucun commentaire:

Enregistrer un commentaire