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");
}
}
}
12Charge soonA 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);
Speed: 120