1 // IfElse.java
2 import java.util.Scanner;
3
4 /** 5 * IfElse testet die Zuordnung von if und else. 6 * Beispielprogramm zur Programmiertechnik 1, Teil 3. 7 * @author H.Drachenfels 8 * @version 17.3.2023 9 */
10 public final class IfElse {
11 private IfElse() { }
12
13 private static final Scanner EINGABE = new Scanner(System.in);
14
15 /** 16 * main ist der Startpunkt des Programms. 17 * @param args wird nicht verwendet. 18 */
19 public static void main(String[] args) {
20 System.out.print("Note als ganze Zahl eingeben: ");
21 int note = EINGABE.nextInt();
22
23 /* 24 * Vorsicht: 25 * Der Compiler ordnet das else dem zweiten if zu. 26 * Er richtet sich nicht nach Einrueckungen. 27 */
28 if (note <= 4)
29 if (note >= 1) System.out.println("bestanden");
30 else
31 System.out.println("nicht bestanden");
32
33 /* 34 * Geschweifte Klammern verhindern Irrtuemer wie oben. 35 */
36 if (note <= 4) {
37 if (note >= 1) System.out.println("bestanden");
38 } else {
39 System.out.println("nicht bestanden");
40 }
41 }
42 }
43