Building a loop program
A larger program is easier to write in stages: get a small part working, check it, then add the next feature. Each stage is a working program, so when something breaks, it's in the part you just added.
The three challenges here build one program. It reads the step counts of a fitness tracker, one day per line, until -1.
- First it only adds up the steps.
- Then it also counts the days and works out the average.
- Finally it counts the days that reached a goal, and finds the best day.
Every stage uses a pattern you've met: a while (true) loop that stops at -1, and a variable before the loop for each thing the program keeps track of. When a later stage adds a feature, it adds a variable before the loop, a line inside it, and a print after it; the rest stays as it was.
Finding the best day is new: keep a best variable, and whenever a day beats it, replace it (if (steps > best) { best = steps; }). Start it at 0, since step counts are never negative.