Java Arena
Step 3 of 4

Read, decide, print

In the second pattern the input decides what the program prints. Between reading and printing there's an if:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int battery = Integer.valueOf(scanner.nextLine());
        if (battery < 20) {
            System.out.println("Charge soon");
        } else {
            System.out.println("Battery OK");
        }
    }
}
Input
12
Output
Charge soon

A decision can also change a value instead of printing: the if fixes up a variable, and one print line after it shows the result. This keeps the printing in one place:

int speed = 140;
if (speed > 120) {
    speed = 120;
}
System.out.println("Speed: " + speed);
Output
Speed: 120

Your task

Challenge 1 of 3

A weather app reads how many degrees the temperature changed since yesterday, negative when it got colder. Print the size of the change, without a minus sign. Use an if for it, not Math.abs, to practice the pattern. For the input -7 the program prints:

Change of 7 degrees

Challenge 2

A volume control goes from 0 to 100. Read a volume setting and keep it in that range: below 0 becomes 0, above 100 becomes 100, and anything else stays. For the input 130 the program prints:

Volume: 100

Challenge 3

Read the points from two rounds of a game and print the total. Then print Level up! when the total is 100 or more, Almost there when it's 80 to 99, and Keep going otherwise. For the input 45 and 60 the program prints:

Total: 105
Level up!