Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4.  
  5. public class prog {
  6.  
  7.  
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.         Scanner s = new Scanner(System.in);
  12.  
  13.         int a = 0; //positiv counter
  14.  
  15.         int b = 0; //negativ counter
  16.  
  17.         int n = s.nextInt();
  18.  
  19.         if (n < 1 || n > 9) {
  20.  
  21.             System.out.println("Eingabe nicht möglich, bitte erneut eingeben! Es sind nur Zahlen zwischen 0 und 10 zugelassen! \n");
  22.  
  23.             n = s.nextInt();
  24.  
  25.         }
  26.  
  27.         float[] array = new float[n];
  28.  
  29.         for (int i = 0; i < array.length; i++) {
  30.  
  31.             float v = s.nextFloat();
  32.  
  33.             if (v > (-100) || v < 100) {
  34.  
  35.                 if (v == 42) {
  36.  
  37.                     array[i] = v;
  38.  
  39.                     System.out.println("Eingabe ist 42, Durchschnitt wird berechnet \n");
  40.  
  41.                     b++;
  42.  
  43.                     break;
  44.  
  45.                 } else {
  46.  
  47.                     array[i] = v;
  48.  
  49.                     if (v > 0) {
  50.  
  51.                         a++;
  52.  
  53.                     } else b++;
  54.  
  55.                 }
  56.  
  57.             }
  58.  
  59.             System.out.println("Durschnitt wird berechnet \n");
  60.  
  61.             float x = 0;
  62.  
  63.             for (int j = 0; j < array.length; j++) {
  64.  
  65.                 x += array[j];
  66.  
  67.             }
  68.  
  69.             x = x / array.length;
  70.  
  71.             System.out.println("Durchschnitt ist: " + x + "\n");
  72.  
  73.             System.out.println("Anzahl positiver Zahlen: " + a + "\n");
  74.  
  75.             System.out.println("Anzahl negativer Zahlen: " + b + "\n");
  76.  
  77.         }
  78.  
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement