All puzzles
Per page
D
Sum ยท Average

Average score

Return the average score of games 1 through games. You may call getScore(int game), which returns that game's score.

If the scores are 10, 15, 12, average(3) returns 12.333....

8 strips: D1 to D8

for (int g = 1; g <= games; g++) {
D1
public static double average(int games) {
D2
}
D3
return (double) total / games;
D4
return total / games;
D5
}
D6
int total = 0;
D7
total += getScore(g);
D8

Answer key: D. Average score

Order: D2, D7, D1, D8, D6, D4, D3. Extra: D5

public static double average(int games) {
	int total = 0;
	for (int g = 1; g <= games; g++) {
		total += getScore(g);
	}
	return (double) total / games;
}

Extra strip: return total / games;. total / games divides two int values, so the decimal part is lost and it returns 12.0.