Combining the patterns
Bigger programs combine the patterns: read several values, calculate from them, decide, and print a sentence that uses the results. Here are two prices, and the program says which is cheaper and by how much:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int shopA = Integer.valueOf(scanner.nextLine());
int shopB = Integer.valueOf(scanner.nextLine());
int difference = Math.abs(shopA - shopB);
if (shopA < shopB) {
System.out.println("Shop A is cheaper by " + difference);
} else if (shopB < shopA) {
System.out.println("Shop B is cheaper by " + difference);
} else {
System.out.println("Same price");
}
}
}
35
29Shop B is cheaper by 6The difference is worked out once, before the decision, and each branch uses it. The same plan works for most problems of this kind: read everything, calculate what the sentences need, then decide which sentence to print.