It looks like that code will print something almost every time through the loop.
I would think you need to do all of the counting first so you know what to print, and then print the totals.
It does...