{"id":"for-loops-structuring","slug":"building-a-loop-program","title":"Building a loop program","text":"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.\n\nThe three challenges here build one program. It reads the step counts of a fitness tracker, one day per line, until `-1`.\n\n1. First it only adds up the steps.\n2. Then it also counts the days and works out the average.\n3. Finally it counts the days that reached a goal, and finds the best day.\n\nEvery 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.\n\nFinding 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.\n","task":"Stage 1. Read step counts until `-1` and print the total. For the input `8000`, `12000`, `5000` and `-1` the program prints:\n\n```\nTotal: 25000\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int total = 0;\n        while (true) {\n            int steps = Integer.valueOf(scanner.nextLine());\n            if (steps == -1) {\n                break;\n            }\n            total += steps;\n        }\n        System.out.println(\"Total: \" + total);\n    }\n}\n","hints":["The summing pattern from module 8: `int total = 0;` before the loop.","Read a number, `break` on -1, otherwise `total += steps;`"],"tests":[{"name":"Test 1","stdin":"8000\n12000\n5000\n-1\n","expect":"Total: 25000","hidden":false},{"name":"Test 2","stdin":"-1\n","expect":"Total: 0","hidden":true},{"name":"Test 3","stdin":"0\n10000\n-1\n","expect":"Total: 10000","hidden":true}],"require":[],"forbid":[],"more":[{"task":"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:\n\n```\nDays: 3\nTotal: 25000\nAverage: 8333.333333333334\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int total = 0;\n        while (true) {\n            int steps = Integer.valueOf(scanner.nextLine());\n            if (steps == -1) {\n                break;\n            }\n            total += steps;\n        }\n        System.out.println(\"Total: \" + total);\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int total = 0;\n        int days = 0;\n        while (true) {\n            int steps = Integer.valueOf(scanner.nextLine());\n            if (steps == -1) {\n                break;\n            }\n            total += steps;\n            days++;\n        }\n        System.out.println(\"Days: \" + days);\n        System.out.println(\"Total: \" + total);\n        System.out.println(\"Average: \" + (double) total / days);\n    }\n}\n","hints":["Add a `days` counter next to `total`, and `days++;` in the loop.","The average after the loop: `(double) total / days`."],"tests":[{"name":"Test 1","stdin":"8000\n12000\n5000\n-1\n","expect":"Days: 3\nTotal: 25000\nAverage: 8333.333333333334","hidden":false},{"name":"Test 2","stdin":"10000\n-1\n","expect":"Days: 1\nTotal: 10000\nAverage: 10000.0","hidden":true},{"name":"Test 3","stdin":"3000\n4000\n-1\n","expect":"Days: 2\nTotal: 7000\nAverage: 3500.0","hidden":true}],"require":[],"forbid":[]},{"task":"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:\n\n```\nDays: 3\nTotal: 25000\nAverage: 8333.333333333334\nDays at 10000 or more: 1\nBest day: 12000\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int total = 0;\n        int days = 0;\n        while (true) {\n            int steps = Integer.valueOf(scanner.nextLine());\n            if (steps == -1) {\n                break;\n            }\n            total += steps;\n            days++;\n        }\n        System.out.println(\"Days: \" + days);\n        System.out.println(\"Total: \" + total);\n        System.out.println(\"Average: \" + (double) total / days);\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int total = 0;\n        int days = 0;\n        int goalDays = 0;\n        int best = 0;\n        while (true) {\n            int steps = Integer.valueOf(scanner.nextLine());\n            if (steps == -1) {\n                break;\n            }\n            total += steps;\n            days++;\n            if (steps >= 10000) {\n                goalDays++;\n            }\n            if (steps > best) {\n                best = steps;\n            }\n        }\n        System.out.println(\"Days: \" + days);\n        System.out.println(\"Total: \" + total);\n        System.out.println(\"Average: \" + (double) total / days);\n        System.out.println(\"Days at 10000 or more: \" + goalDays);\n        System.out.println(\"Best day: \" + best);\n    }\n}\n","hints":["Two more variables before the loop: a counter for goal days and `int best = 0;`.","In the loop: `if (steps >= 10000) { goalDays++; }` and `if (steps > best) { best = steps; }`."],"tests":[{"name":"Test 1","stdin":"8000\n12000\n5000\n-1\n","expect":"Days: 3\nTotal: 25000\nAverage: 8333.333333333334\nDays at 10000 or more: 1\nBest day: 12000","hidden":false},{"name":"Test 2","stdin":"10000\n10000\n-1\n","expect":"Days: 2\nTotal: 20000\nAverage: 10000.0\nDays at 10000 or more: 2\nBest day: 10000","hidden":true},{"name":"Test 3","stdin":"2500\n9999\n700\n-1\n","expect":"Days: 3\nTotal: 13199\nAverage: 4399.666666666667\nDays at 10000 or more: 0\nBest day: 9999","hidden":true}],"require":[],"forbid":[]}]}