I have this code for returning percentage of grades less than 60. But it doesn’t result in the desired percent. Someone help me figure out what’s wrong.
public float getPercentFailing(float[] grades) {
float sum= 0;
for (int i = 0; i < .6; i++) {
sum = sum + grades[i];
return sum/100;
}
return 0;
}
Try something like this. You need to first add all of the grades up, which is what you’re doing in the for loop. Now since we only want the grades over 60 we use an
ifstatement to check if the value is above 60. If it is, add it to the sum and tally one toint countto keep track of how many grades we’ve added so far.Then divide the sum of the grades by the total number of grades, or
grades.lengthandreturnit.Everything together now: