Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Notenberechnung {
- static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) {
- int anzahl = leseNotenAnzahl();
- double[] noten = leseNoten(anzahl);
- zeigeNoten(noten);
- double durchschnitt = berechneDurchschnitt(noten);
- System.out.println("Notendurchschnitt: " + durchschnitt);
- }
- public static int leseNotenAnzahl() {
- System.out.print("Notenanzahl eingeben:");
- int anzahl = sc.nextInt();//TODO: Fehlerabfrage
- return anzahl;
- }
- public static double[] leseNoten(int anzahl) {
- double[] noten = new double[anzahl];
- for(int i = 0; i < anzahl; i++) {
- System.out.print("Note " + (i+1) + ": ");
- noten[i] = sc.nextDouble();
- }
- return noten;
- }
- public static void zeigeNoten(double[] noten) {
- System.out.println("Noten:");
- for(int i = 0; i < noten.length; i++) {
- System.out.println("Note " + (i+1) + " = " + noten[i]);
- }
- }
- public static double berechneDurchschnitt(double[] noten) {
- if(noten.length == 0)
- return Double.NaN;
- double durchschnitt = 0;
- for(int i = 0; i < noten.length; i++) {
- durchschnitt += noten[i];
- }
- return durchschnitt / noten.length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement