This commit is contained in:
Luca Conte 2022-11-10 18:13:12 +01:00
parent 5dcfdc6f4f
commit 7089ac2ab0
2 changed files with 34 additions and 0 deletions

BIN
uebungen/u16/Range.class Normal file

Binary file not shown.

34
uebungen/u16/Range.java Normal file
View File

@ -0,0 +1,34 @@
/**
* Klasse zum ausgeben von Zahlenreihen mit angebrachter Formatierung
*/
public class Range {
public static void main(String[] args) {
printRange(2, 7);
printRange(19, 11);
printRange(5, 5);
}
/**
* Gibt eine Zahlenfolge von from bis to in eckigen Klammern und einzeln durch Kommas getrennt aus.
* Ausgabe in aufsteigender Reihenfolge, falls from > to, andernfalls absteigend.
*/
public static void printRange(int from, int to) {
System.out.print("[" + from);
if (to > from) {
for (int i = from + 1; i <= to; i++) {
printNumber(i);
}
} else {
for (int i = from - 1; i >= to; i--) {
printNumber(i);
}
}
System.out.println("]");
}
/**
* Gibt eine Zahl nach einem Komma und einem Leerzeichen ausgeben
*/
public static void printNumber(int num) {
System.out.print(", " + num);
}
}