Java Arena
Step 7 of 7

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.

  1. First it only adds up the steps.
  2. Then it also counts the days and works out the average.
  3. 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.

Your task

Challenge 1 of 3

Stage 1. Read step counts until -1 and print the total. For the input 8000, 12000, 5000 and -1 the program prints:

Total: 25000

Challenge 2

Stage 2. Also count the days and print the average steps per day. There's always at least one day. For the input 8000, 12000, 5000 and -1 the program prints:

Days: 3
Total: 25000
Average: 8333.333333333334

Challenge 3

Stage 3. Also print how many days reached the goal of 10000 steps, and the best day. For the input 8000, 12000, 5000 and -1 the program prints:

Days: 3
Total: 25000
Average: 8333.333333333334
Days at 10000 or more: 1
Best day: 12000