{"id":"loops-summing","slug":"summing","title":"Summing in a loop","text":"Summing works like counting, but instead of adding one each time, the loop adds the value it read. The sum starts at 0 before the loop:\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 total = 0;\n        while (true) {\n            int price = Integer.valueOf(scanner.nextLine());\n            if (price == 0) {\n                break;\n            }\n            total += price;\n        }\n        System.out.println(\"To pay: \" + total);\n    }\n}\n```\n\n```input\n4\n12\n3\n0\n```\n\n```output\nTo pay: 19\n```\n\n`total += price` is shorthand for `total = total + price`. The other operators have one too: `-=`, `*=` and `/=`.\n\nA loop can keep several of these at once, such as a count and a sum, each in its own variable made before the loop.\n","task":"Read the lengths of your runs in kilometres until `0`, and print the total distance. For the input `5`, `10`, `8` and `0` the program prints:\n\n```\nTotal: 23 km\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 run = Integer.valueOf(scanner.nextLine());\n            if (run == 0) {\n                break;\n            }\n            total += run;\n        }\n        System.out.println(\"Total: \" + total + \" km\");\n    }\n}\n","hints":["`int total = 0;` before the loop; `total += run;` inside it.","Print the total after the loop."],"tests":[{"name":"Test 1","stdin":"5\n10\n8\n0\n","expect":"Total: 23 km","hidden":false},{"name":"Test 2","stdin":"0\n","expect":"Total: 0 km","hidden":true},{"name":"Test 3","stdin":"42\n0\n","expect":"Total: 42 km","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Read donations until `0`. Print how many donations there were and their total. For the input `20`, `5`, `50` and `0` the program prints:\n\n```\n3 donations, 75 in total\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 total = 0;\n        while (true) {\n            int donation = Integer.valueOf(scanner.nextLine());\n            if (donation == 0) {\n                break;\n            }\n            count++;\n            total += donation;\n        }\n        System.out.println(count + \" donations, \" + total + \" in total\");\n    }\n}\n","hints":["Two variables before the loop, a count and a total.","Each round adds one to the count and the donation to the total."],"tests":[{"name":"Test 1","stdin":"20\n5\n50\n0\n","expect":"3 donations, 75 in total","hidden":false},{"name":"Test 2","stdin":"100\n0\n","expect":"1 donations, 100 in total","hidden":true},{"name":"Test 3","stdin":"0\n","expect":"0 donations, 0 in total","hidden":true}],"require":[],"forbid":[]},{"task":"Read the money moving in and out of an account until `0`. Positive amounts are income, negative amounts are spending. Print the income and the spending, the spending as a positive number. For the input `500`, `-40`, `-15` and `0` the program prints:\n\n```\nIncome: 500\nSpending: 55\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 income = 0;\n        int spending = 0;\n        while (true) {\n            int amount = Integer.valueOf(scanner.nextLine());\n            if (amount == 0) {\n                break;\n            }\n            if (amount > 0) {\n                income += amount;\n            } else {\n                spending -= amount;\n            }\n        }\n        System.out.println(\"Income: \" + income);\n        System.out.println(\"Spending: \" + spending);\n    }\n}\n","hints":["Two sums, and an `if` decides which one each amount goes to.","Spending is negative, so subtract it to get a positive sum: `spending -= amount;`"],"tests":[{"name":"Test 1","stdin":"500\n-40\n-15\n0\n","expect":"Income: 500\nSpending: 55","hidden":false},{"name":"Test 2","stdin":"-3\n-4\n0\n","expect":"Income: 0\nSpending: 7","hidden":true},{"name":"Test 3","stdin":"10\n20\n-5\n30\n0\n","expect":"Income: 60\nSpending: 5","hidden":true}],"require":[],"forbid":[]}]}