← all problems

printAverage 🌶️

The method below is supposed to calculate the average number of points scored per game and print it. It compiles and runs — but the printed answer is wrong. totalPoints and numberOfGames are both declared as int, and dividing an int by an int in Java always produces an int result — any remainder is thrown away during the division itself, before the result is ever stored in a double. To get a precise decimal answer, at least one of the two values being divided needs to already be a double before the division happens. Fix the code using a cast so the division itself is done with real (floating-point) division, not integer division.