Java Arena
Step 5 of 6

else if chains

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.

int temperature = 12;
if (temperature > 25) {
    System.out.println("Hot");
} else if (temperature > 15) {
    System.out.println("Warm");
} else if (temperature > 5) {
    System.out.println("Cool");
} else {
    System.out.println("Cold");
}
Output
Cool

For 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.

Your task

Challenge 1 of 3

Read a number and print whether it's Positive, Negative or Zero. For the input -3 the program prints:

Negative

Challenge 2

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:

Player 2 wins

Challenge 3

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:

The largest is 11