{"id":"conditionals-else-if","slug":"else-if","title":"else if chains","text":"With more than two cases, chain conditions with `else if`. Java checks them from the top, runs the block of the **first** condition that's true, and skips the rest. A final `else` catches everything else.\n\n```java\nint temperature = 12;\nif (temperature > 25) {\n    System.out.println(\"Hot\");\n} else if (temperature > 15) {\n    System.out.println(\"Warm\");\n} else if (temperature > 5) {\n    System.out.println(\"Cool\");\n} else {\n    System.out.println(\"Cold\");\n}\n```\n\n```output\nCool\n```\n\nFor 12, the first two conditions are false and the third is true, so the program prints `Cool` and the rest of the chain is skipped. Exactly one block of the chain runs. The final `else` is optional; without it, no block runs when no condition is true.\n","task":"Read a number and print whether it's `Positive`, `Negative` or `Zero`. For the input `-3` the program prints:\n\n```\nNegative\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 number = Integer.valueOf(scanner.nextLine());\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 number = Integer.valueOf(scanner.nextLine());\n        if (number > 0) {\n            System.out.println(\"Positive\");\n        } else if (number < 0) {\n            System.out.println(\"Negative\");\n        } else {\n            System.out.println(\"Zero\");\n        }\n    }\n}\n","hints":["Three cases: `if (number > 0)`, `else if (number < 0)`, and `else`.","When the number is neither greater nor less than 0, it's 0."],"tests":[{"name":"Test 1","stdin":"-3\n","expect":"Negative","hidden":false},{"name":"Test 2","stdin":"0\n","expect":"Zero","hidden":true},{"name":"Test 3","stdin":"8\n","expect":"Positive","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Two players finish a game. Read player 1's score, then player 2's, and print `Player 1 wins`, `Player 2 wins` or `Draw`. For the input `4` and `9` the program prints:\n\n```\nPlayer 2 wins\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 first = Integer.valueOf(scanner.nextLine());\n        int second = Integer.valueOf(scanner.nextLine());\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 first = Integer.valueOf(scanner.nextLine());\n        int second = Integer.valueOf(scanner.nextLine());\n        if (first > second) {\n            System.out.println(\"Player 1 wins\");\n        } else if (second > first) {\n            System.out.println(\"Player 2 wins\");\n        } else {\n            System.out.println(\"Draw\");\n        }\n    }\n}\n","hints":["Three cases: `first > second`, `second > first`, and neither.","The last case needs no condition: `else` catches the draw."],"tests":[{"name":"Test 1","stdin":"4\n9\n","expect":"Player 2 wins","hidden":false},{"name":"Test 2","stdin":"9\n4\n","expect":"Player 1 wins","hidden":true},{"name":"Test 3","stdin":"6\n6\n","expect":"Draw","hidden":true}],"require":[],"forbid":[]},{"task":"Read three numbers and print the largest. When two or more share the largest value, print it just once. For the input `3`, `11` and `7` the program prints:\n\n```\nThe largest is 11\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 a = Integer.valueOf(scanner.nextLine());\n        int b = Integer.valueOf(scanner.nextLine());\n        int c = Integer.valueOf(scanner.nextLine());\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 a = Integer.valueOf(scanner.nextLine());\n        int b = Integer.valueOf(scanner.nextLine());\n        int c = Integer.valueOf(scanner.nextLine());\n        int largest = a;\n        if (b > largest) {\n            largest = b;\n        }\n        if (c > largest) {\n            largest = c;\n        }\n        System.out.println(\"The largest is \" + largest);\n    }\n}\n","hints":["One way: start with `int largest = a;` and replace it when `b` or `c` is bigger.","`>` or `>=` both work here: when two numbers are equal, either one is the largest."],"tests":[{"name":"Test 1","stdin":"3\n11\n7\n","expect":"The largest is 11","hidden":false},{"name":"Test 2","stdin":"5\n5\n2\n","expect":"The largest is 5","hidden":true},{"name":"Test 3","stdin":"-1\n-8\n-1\n","expect":"The largest is -1","hidden":true},{"name":"Test 4","stdin":"1\n2\n30\n","expect":"The largest is 30","hidden":true}],"require":[],"forbid":[]}]}