{"id":"loops-averages","slug":"averages","title":"Averages after the loop","text":"An average needs both a sum and a count: it's the sum divided by the count. The loop collects the two, and the average is calculated **after** the loop, once all the numbers are in:\n\n```java\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int count = 0;\n        int sum = 0;\n        while (true) {\n            int number = Integer.valueOf(scanner.nextLine());\n            if (number == 0) {\n                break;\n            }\n            count++;\n            sum += number;\n        }\n        if (count == 0) {\n            System.out.println(\"No numbers\");\n        } else {\n            double average = (double) sum / count;\n            System.out.println(\"Average: \" + average);\n        }\n    }\n}\n```\n\n```input\n4\n5\n8\n0\n```\n\n```output\nAverage: 5.666666666666667\n```\n\nTwo details matter:\n\n- `(double) sum / count` turns the sum into a `double` before dividing, so the decimals are kept (module 4 showed why).\n- With no numbers at all, `count` is 0, and there's no average to show. The `if` checks for that before dividing.\n","task":"Read ratings from 1 to 5 until `0` (there's always at least one rating), and print their average. For the input `4`, `5`, `3` and `0` the program prints:\n\n```\nAverage rating: 4.0\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 count = 0;\n        int sum = 0;\n        while (true) {\n            int rating = Integer.valueOf(scanner.nextLine());\n            if (rating == 0) {\n                break;\n            }\n            count++;\n            sum += rating;\n        }\n        double average = (double) sum / count;\n        System.out.println(\"Average rating: \" + average);\n    }\n}\n","hints":["A count and a sum in the loop, like the donations challenge.","After the loop: `double average = (double) sum / count;`"],"tests":[{"name":"Test 1","stdin":"4\n5\n3\n0\n","expect":"Average rating: 4.0","hidden":false},{"name":"Test 2","stdin":"5\n4\n0\n","expect":"Average rating: 4.5","hidden":true},{"name":"Test 3","stdin":"1\n0\n","expect":"Average rating: 1.0","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Read temperatures until `-999`. Print the average of the ones above 0 as `Average of warm days: X`, or `No warm days` when there were none. For the input `12`, `-3`, `7` and `-999` the program prints:\n\n```\nAverage of warm days: 9.5\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 count = 0;\n        int sum = 0;\n        while (true) {\n            int temperature = Integer.valueOf(scanner.nextLine());\n            if (temperature == -999) {\n                break;\n            }\n            if (temperature > 0) {\n                count++;\n                sum += temperature;\n            }\n        }\n        if (count == 0) {\n            System.out.println(\"No warm days\");\n        } else {\n            System.out.println(\"Average of warm days: \" + (double) sum / count);\n        }\n    }\n}\n","hints":["Count and sum only the temperatures above 0.","After the loop, check `count == 0` before dividing."],"tests":[{"name":"Test 1","stdin":"12\n-3\n7\n-999\n","expect":"Average of warm days: 9.5","hidden":false},{"name":"Test 2","stdin":"-5\n0\n-999\n","expect":"No warm days","hidden":true},{"name":"Test 3","stdin":"20\n-999\n","expect":"Average of warm days: 20.0","hidden":true}],"require":[],"forbid":[]},{"task":"A survey collects answers, `yes` or `no`, until `stop`. Print the share of `yes` answers as a percentage, calculated as 100.0 times the yes answers divided by all answers. There's always at least one answer. For the input `yes`, `yes`, `no`, `yes` and `stop` the program prints:\n\n```\n75.0% said yes\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 answers = 0;\n        int yes = 0;\n        while (true) {\n            String answer = scanner.nextLine();\n            if (answer.equals(\"stop\")) {\n                break;\n            }\n            answers++;\n            if (answer.equals(\"yes\")) {\n                yes++;\n            }\n        }\n        double percentage = 100.0 * yes / answers;\n        System.out.println(percentage + \"% said yes\");\n    }\n}\n","hints":["Two counters, all answers and the yes answers.","`100.0 * yes / answers` starts with a `double`, so the division keeps its decimals."],"tests":[{"name":"Test 1","stdin":"yes\nyes\nno\nyes\nstop\n","expect":"75.0% said yes","hidden":false},{"name":"Test 2","stdin":"no\nstop\n","expect":"0.0% said yes","hidden":true},{"name":"Test 3","stdin":"yes\nno\nstop\n","expect":"50.0% said yes","hidden":true}],"require":[],"forbid":[]}]}