Java Arena
Step 2 of 6

Blocks and indentation

A block is the code between a { and its }. The class, the main method and each if have one, and blocks sit inside each other.

Java itself doesn't care about spaces at the start of a line. People do: indentation shows at a glance which lines are inside which block. The usual rule, used throughout this site:

  • every line inside a block is indented 4 spaces more than the line that opened the block;
  • the closing } lines up with the line that opened the block.
public class Main {
    public static void main(String[] args) {
        int points = 12;
        if (points > 10) {
            System.out.println("Level up!");
        }
        System.out.println("Points: " + points);
    }
}
Output
Level up!
Points: 12

The editor helps: after a {, pressing Enter indents the next line, and the Tab key (also on the symbol bar on phones) adds 4 spaces.

Checking the style

In the first two challenges of this step, Check also checks the indentation. Everywhere else, indentation that doesn't match the braces shows up as a note after the tests, but doesn't stop a challenge from passing.

Your task

Challenge 1 of 3

This program works, but its indentation is a mess. Re-indent every line so it follows the rule. The output stays:

Warm day
Have a nice day

Challenge 2

Re-indent this program too. It reads a number and checks it twice. For the input 250 the output stays:

Big number
Checked

Challenge 3

The indentation in this program makes it look as if both extra lines belong to the inner if, but only the first one does: an if without braces controls just one statement. Add braces, so both lines are printed only when the points are over 20. For the input 25 the program prints:

Level up!
Bonus!
Double bonus!

For 15 it prints only Level up!.