Advertisement
Guest User

Untitled

a guest
Dec 21st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Notenberechnung {
  4.  
  5. static Scanner sc = new Scanner(System.in);
  6.  
  7. public static void main(String[] args) {
  8. int anzahl = leseNotenAnzahl();
  9. double[] noten = leseNoten(anzahl);
  10. zeigeNoten(noten);
  11. double durchschnitt = berechneDurchschnitt(noten);
  12. System.out.println("Notendurchschnitt: " + durchschnitt);
  13. }
  14.  
  15. public static int leseNotenAnzahl() {
  16. System.out.print("Notenanzahl eingeben:");
  17.  
  18. int anzahl = sc.nextInt();//TODO: Fehlerabfrage
  19.  
  20. return anzahl;
  21. }
  22.  
  23. public static double[] leseNoten(int anzahl) {
  24. double[] noten = new double[anzahl];
  25.  
  26. for(int i = 0; i < anzahl; i++) {
  27. System.out.print("Note " + (i+1) + ": ");
  28. noten[i] = sc.nextDouble();
  29. }
  30.  
  31. return noten;
  32. }
  33.  
  34. public static void zeigeNoten(double[] noten) {
  35. System.out.println("Noten:");
  36. for(int i = 0; i < noten.length; i++) {
  37. System.out.println("Note " + (i+1) + " = " + noten[i]);
  38. }
  39. }
  40.  
  41. public static double berechneDurchschnitt(double[] noten) {
  42. if(noten.length == 0)
  43. return Double.NaN;
  44.  
  45. double durchschnitt = 0;
  46. for(int i = 0; i < noten.length; i++) {
  47. durchschnitt += noten[i];
  48. }
  49.  
  50. return durchschnitt / noten.length;
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement