1 // FallunterscheidungIfElse.java
2 import java.util.Scanner;
3
4 /** 5 * FallunterscheidungIfElse gibt die Anzahl der Tage eines Monats aus. 6 * Umformulierung des switch-case-Programms mit if-else. 7 * Beispielprogramm zur Programmiertechnik 1, Teil 3. 8 * @author H.Drachenfels 9 * @version 9.1.2019 10 */
11 public final class FallunterscheidungIfElse {
12 private FallunterscheidungIfElse() { }
13
14 private static final Scanner EINGABE = new Scanner(System.in);
15
16 /** 17 * main ist der Startpunkt des Programms. 18 * @param args wird nicht verwendet. 19 */
20 public static void main(String[] args) {
21 System.out.print("Monat eingeben [1-12]: ");
22 int month = EINGABE.hasNextInt() ? EINGABE.nextInt() : 0;
23
24 if (month == 2) {
25 System.out.println("28 oder 29 Tage");
26 } else if (month == 4 || month == 6 || month == 9 || month == 11) {
27 System.out.println("30 Tage");
28 } else if (month == 1
29 || month == 3
30 || month == 5
31 || month == 7
32 || month == 8
33 || month == 10
34 || month == 12
35 /* oder einfacher: month >= 1 && month <= 12 */) {
36 System.out.println("31 Tage");
37 } else {
38 System.err.println("Eingabefehler!");
39 }
40 }
41 }
42