Einlesen aus messwerte.txt eingebaut

main
Alexander Orsini 2026-11-12 16:05:54 +01:00
parent 522bb0f9f6
commit e8b94ac0a6
2 changed files with 16 additions and 5 deletions

View File

@ -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: ");
}
}

2
messwerte.txt 100644
View File

@ -0,0 +1,2 @@
(12, -4, 19, 0, -1, 23, 15, 8)