Java Arena
Step 6 of 6

The first true branch wins

In an else if chain only the first true condition counts, so the order of the conditions changes what the program does. Look at this chain, meant to give a grade:

int points = 95;
if (points >= 50) {
    System.out.println("Passed");
} else if (points >= 90) {
    System.out.println("Excellent");
}
Output
Passed

95 should be Excellent, but points >= 50 is checked first, it's true, and the rest of the chain is skipped. Excellent can never be printed. Checking the strictest condition first fixes it:

int points = 95;
if (points >= 90) {
    System.out.println("Excellent");
} else if (points >= 50) {
    System.out.println("Passed");
}
Output
Excellent

When conditions overlap, put the most specific one first. Each else if can then rely on the earlier conditions having been false: in the second version, the >= 50 branch only ever sees points below 90.

Your task

Challenge 1 of 3

This chain is in the wrong order, so it never prints Gold. Reorder the branches, so 1000 points or more gives Gold, 500 or more Silver, and anything less Bronze. For the input 1200 the program prints Gold.

Challenge 2

Read a score from 0 to 100 and print its band: 90 to 100 is A, 70 to 89 is B, 50 to 69 is C, and below 50 is D. For the input 75 the program prints:

B

Challenge 3

A finer scale, and a check on the input. Scores go from 0 to 100; anything else is Invalid score. The grades (an invented scale for this exercise):

Score Grade
95 to 100 A+
85 to 94 A
75 to 84 B
65 to 74 C
55 to 64 D
45 to 54 E
0 to 44 F

For the input 88 the program prints:

A