diff --git a/Datenanalyse.java b/Datenanalyse.java index 24ceff7..e57aa49 100644 --- a/Datenanalyse.java +++ b/Datenanalyse.java @@ -1,15 +1,21 @@ -void main() { +import java.util.Scanner; +import java.io.File; + +void main() throws Exception { int summe = 0; int anzahl = 0; int maximum = Integer.MIN_VALUE; -while (true) { -int eingabe = Integer.parseInt(IO.readln("Messwert eingeben (-1 zum Beenden): ")); +Scanner sc = new Scanner(new File("messwerte.txt"), "UTF-8"); -if (eingabe == -1) { -break; +while (sc.hasNextLine()) { +String zeile = sc.nextLine(); +if (zeile.isBlank()) { +continue; } +int eingabe = Integer.parseInt(zeile); + summe = summe + eingabe; anzahl = anzahl + 1; @@ -18,6 +24,8 @@ maximum = eingabe; } } +sc.close(); + if (anzahl >= 1) { double durchschnitt = (double) summe / anzahl; IO.println("Anzahl: " + anzahl); @@ -25,4 +33,5 @@ IO.println("Durchschnitt: " + durchschnitt); IO.println("Maximum: " + maximum); } else { IO.println("Keine Daten eingegeben: "); +} } \ No newline at end of file diff --git a/messwerte.txt b/messwerte.txt new file mode 100644 index 0000000..4fb3587 --- /dev/null +++ b/messwerte.txt @@ -0,0 +1,2 @@ +(12, -4, 19, 0, -1, 23, 15, 8) +