Statistik und Einlese-Methoden verbunden

main
Alexander Orsini 2026-11-12 18:51:53 +01:00
parent 61d8998ef8
commit c1099860ab
2 changed files with 85 additions and 60 deletions

View File

@ -1,37 +1,53 @@
import java.util.Scanner; import java.util.Scanner;
import java.io.File; import java.io.File;
int zaehleWerte(String dateiname) throws Exception {
Scanner sc = new Scanner(new File(dateiname), "UTF-8");
int anzahl = 0;
while (sc.hasNextLine()) {
String zeile = sc.nextLine();
if (!zeile.isBlank()) {
anzahl = anzahl + 1;
}
}
sc.close();
return anzahl;
}
int[] ladeMesswerte(String dateiname) throws Exception {
int anzahl = zaehleWerte(dateiname);
int[] werte = new int[anzahl];
Scanner sc = new Scanner(new File(dateiname), "UTF-8");
int index = 0;
while (sc.hasNextLine()) {
String zeile = sc.nextLine();
if (!zeile.isBlank()) {
werte[index] = Integer.parseInt(zeile);
index = index + 1;
}
}
sc.close();
return werte;
}
void main() throws Exception { void main() throws Exception {
int summe = 0; int[] werte = ladeMesswerte("messwerte.txt");
int anzahl = 0;
int maximum = Integer.MIN_VALUE;
Scanner sc = new Scanner(new File("messwerte.txt"), "UTF-8"); if (werte.length >= 1) {
Statistik s = new Statistik();
int summe = s.berechneSumme(werte);
double durchschnitt = s.berechneDurchschnitt(werte);
int min = s.findeMinimum(werte);
int max = s.findeMaximum(werte);
int ueberSchnitt = s.zaehleUeberDurchschnitt(werte, durchschnitt);
while (sc.hasNextLine()) { IO.println("Anzahl: " + werte.length);
String zeile = sc.nextLine(); IO.println("Summe: " + summe);
if (zeile.isBlank()) { IO.println("Durchschnitt: " + durchschnitt);
continue; IO.println("Minimum: " + min);
} IO.println("Maximum: " + max);
IO.println("Ueber Durchschnitt: " + ueberSchnitt);
int eingabe = Integer.parseInt(zeile); } else {
IO.println("Keine Daten gefunden.");
summe = summe + eingabe; }
anzahl = anzahl + 1;
if (eingabe > maximum) {
maximum = eingabe;
}
}
sc.close();
if (anzahl >= 1) {
double durchschnitt = (double) summe / anzahl;
IO.println("Anzahl: " + anzahl);
IO.println("Durchschnitt: " + durchschnitt);
IO.println("Maximum: " + maximum);
} else {
IO.println("Keine Daten eingegeben: ");
}
} }

View File

@ -1,34 +1,43 @@
class Statistik { class Statistik {
int berechneSumme(int[] werte) { int berechneSumme(int[] werte) {
int summe = 0; int summe = 0;
for (int i = 0; i < werte.length; i++) { for (int i = 0; i < werte.length; i++) {
summe = summe + werte[i]; summe = summe + werte[i];
} }
return summe; return summe;
} }
double berechneDurchschnitt(int[] werte; double berechneDurchschnitt(int[] werte) {
{ return (double) berechneSumme(werte) / werte.length;
return(double); }
berechneSumme(werte) / werte.length;
}
int findeMinimum(int[] werte) { int findeMinimum(int[] werte) {
int minimum = Integer.MAX_VALUE; int minimum = Integer.MAX_VALUE;
for (int i = 0; i < werte.length; i++) { for (int i = 0; i < werte.length; i++) {
if (werte[i] > maximum) { if (werte[i] < minimum) {
maximum = werte[i]; minimum = werte[i];
} }
} }
return maximum; return minimum;
} }
int zaehleUeberDurchschnitt(int[] werte, double durchschnitt) { int findeMaximum(int[] werte) {
int treffer = 0; int maximum = Integer.MIN_VALUE;
for (int i = 0; i < werte.length; i++) { for (int i = 0; i < werte.length; i++) {
if (werte[i] > durchschnitt) { if (werte[i] > maximum) {
treffer = treffer + 1; maximum = werte[i];
} }
return treffer; }
} return maximum;
}
int zaehleUeberDurchschnitt(int[] werte, double durchschnitt) {
int treffer = 0;
for (int i = 0; i < werte.length; i++) {
if (werte[i] > durchschnitt) {
treffer = treffer + 1;
}
}
return treffer;
}
} }