If Verzweigung: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Keine Bearbeitungszusammenfassung Markierung: Manuelle Zurücksetzung |
Keine Bearbeitungszusammenfassung |
||
Zeile 1: | Zeile 1: | ||
Verzweigungen | public class Verzweigungen { | ||
public static void main(String[] args) { | |||
// Beispiel für if-Anweisung | |||
int zahl = 10; | |||
if (zahl > 5) { | |||
System.out.println("Die Zahl ist größer als 5."); | |||
} | |||
// Beispiel für if-else-Anweisung | |||
int alter = 18; | |||
if (alter >= 18) { | |||
System.out.println("Du bist volljährig."); | |||
} else { | |||
System.out.println("Du bist nicht volljährig."); | |||
} | |||
2. if | // Beispiel für else if-Anweisung | ||
int note = 3; | |||
if (note == 1) { | |||
System.out.println("Sehr gut!"); | |||
} else if (note == 2) { | |||
System.out.println("Gut."); | |||
} else if (note == 3) { | |||
System.out.println("Befriedigend."); | |||
} else if (note == 4) { | |||
System.out.println("Ausreichend."); | |||
} else if (note == 5) { | |||
System.out.println("Mangelhaft."); | |||
} else { | |||
System.out.println("Ungenügend."); | |||
} | |||
// Beispiel für switch-Anweisung | |||
char tag = 'M'; | |||
switch (tag) { | |||
case 'M': | |||
System.out.println("Heute ist Montag."); | |||
break; | |||
case 'D': | |||
System.out.println("Heute ist Dienstag."); | |||
break; | |||
case 'M': | |||
System.out.println("Heute ist Mittwoch."); | |||
break; | |||
case 'D': | |||
System.out.println("Heute ist Donnerstag."); | |||
break; | |||
case 'F': | |||
System.out.println("Heute ist Freitag."); | |||
break; | |||
case 'S': | |||
System.out.println("Heute ist Samstag."); | |||
break; | |||
case 'S': | |||
System.out.println("Heute ist Sonntag."); | |||
break; | |||
default: | |||
System.out.println("Ungültiger Tag."); | |||
break; | |||
} | |||
} | |||
} |
Version vom 1. Juli 2024, 10:01 Uhr
public class Verzweigungen {
public static void main(String[] args) { // Beispiel für if-Anweisung int zahl = 10; if (zahl > 5) { System.out.println("Die Zahl ist größer als 5."); }
// Beispiel für if-else-Anweisung int alter = 18; if (alter >= 18) { System.out.println("Du bist volljährig."); } else { System.out.println("Du bist nicht volljährig."); }
// Beispiel für else if-Anweisung int note = 3; if (note == 1) { System.out.println("Sehr gut!"); } else if (note == 2) { System.out.println("Gut."); } else if (note == 3) { System.out.println("Befriedigend."); } else if (note == 4) { System.out.println("Ausreichend."); } else if (note == 5) { System.out.println("Mangelhaft."); } else { System.out.println("Ungenügend."); }
// Beispiel für switch-Anweisung char tag = 'M'; switch (tag) { case 'M': System.out.println("Heute ist Montag."); break; case 'D': System.out.println("Heute ist Dienstag."); break; case 'M': System.out.println("Heute ist Mittwoch."); break; case 'D': System.out.println("Heute ist Donnerstag."); break; case 'F': System.out.println("Heute ist Freitag."); break; case 'S': System.out.println("Heute ist Samstag."); break; case 'S': System.out.println("Heute ist Sonntag."); break; default: System.out.println("Ungültiger Tag."); break; } }
}