Advertisement
Guest User

clike Beispiel

a guest
Oct 14th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.89 KB | None | 0 0
  1. package steemit; //das so gennante Packet des Projekts wo der Code drinne ist
  2.  
  3. import java.util.Scanner; // Bibliothek für das Scanner Objekt
  4.  
  5. public class clike { // "clike" Klasse. Die .java Datei sollte "clike.java" heißen
  6.  
  7.     public static void main(String[] args) { // die Main-Funktion
  8.         // Variablen Deklarieren
  9.         Scanner in = new Scanner(System.in); // Scanner Objekt
  10.         int[] A; // Ganzzahl-Feld deklarieren
  11.         int size; // Feldgrößen-Variable
  12.         int a, b; // Werte für "fill"
  13.         int sum = -1, max, min; // Summe, Maximal und Minimal Wert
  14.         // sum ist "-1" so das wir kontrollieren ob es bereits ausfgeführt wurde
  15.         int search, num, temp; // Eingabe vom Benutzer
  16.         double avg = -1; // Durchschnitt auch "-1"
  17.         char sort; // für die Sortierungsweise
  18.         String choice; // Zeichenkette für Switch-Case
  19.  
  20.         // Feldgröße als Eingabe nehmen
  21.         do {
  22.             System.out.print("Give size for array: "); // Ausgabe
  23.             size = in.nextInt(); // Ganzzahl Eingabe
  24.             // Kontrollieren ob der Wert in 1...100 ist.
  25.             if (size < 1 || size > 100)
  26.                 // "Fehler" Meldung
  27.                 System.out.println("The size must be in the range of 1...100");
  28.         } while (size < 1 || size > 100);
  29.         A = new int[size]; // Feld Objekt erstellen-zuweisen
  30.  
  31.         do { // Unendlicher Loop
  32.             // Menü Ausgabe
  33.             System.out.println("“fill”: fill Array with rand numbers");
  34.             System.out.println("“sum” : sum Array");
  35.             System.out.println("“max” : find max");
  36.             System.out.println("“min” : find min");
  37.             System.out.println("“sort” : sort Array");
  38.             System.out.println("“search” : search number");
  39.             System.out.println("“avg” : average");
  40.             System.out.println("“Q” : implementation of question Q");
  41.             System.out.println("“exit” : Exit");
  42.             // Auswahl-Eingabe
  43.             System.out.print("choice: ");
  44.             choice = in.next(); // Zeichenketten-Eingabe
  45.            
  46.             // switch-case von einem String! Das konnte man bei C nicht machen!
  47.             switch (choice) {  
  48.             case "fill":
  49.                 // a und b Grenzen Eingabe
  50.                 do {
  51.                     System.out.println("Give 2 ints to fill array with random numbers between them");
  52.                     System.out.print("a: ");
  53.                     a = in.nextInt();
  54.                     System.out.print("b: ");
  55.                     b = in.nextInt();
  56.                     // kontrollieren ob a < b ist
  57.                     if (!(a < b))
  58.                         System.out.println("a must be less then b");
  59.                 } while (!(a < b));
  60.  
  61.                 // Feld mit Ganzzahlen in [a, b] füllen
  62.                 for (int i = 0; i < A.length; i++) {
  63.                     // Zufällige Nummer in a...b
  64.                     A[i] = a + (int) ((b - a + 1) * Math.random());
  65.                 }
  66.                 break;
  67.             case "sum":
  68.                 // Summe berechnen
  69.                 sum = 0;
  70.                 for (int i = 0; i < A.length; i++) {
  71.                     sum += A[i];
  72.                 }
  73.                 System.out.println("sum of array is: " + sum);
  74.                 break;
  75.             case "max":
  76.                 // Maximal-Wert finden
  77.                 max = 0;
  78.                 for (int i = 1; i < A.length; i++) {
  79.                     if (A[i] > A[max])
  80.                         max = i;
  81.                 }
  82.                 System.out.println("max is " + A[max]);
  83.                 break;
  84.             case "min":
  85.                 // Minimal-Wert finden
  86.                 min = 0;
  87.                 for (int i = 1; i < A.length; i++) {
  88.                     if (A[i] < A[min])
  89.                         min = i;
  90.                 }
  91.                 System.out.println("min is " + A[min]);
  92.                 break;
  93.             case "sort":sorting array
  94.                 // Feld Sortieren
  95.                 // Sortier-Weise Eingeben
  96.                 System.out.println("Do you want ascending (a) or descending (d) order?");
  97.                 do {
  98.                     System.out.print("Choice: ");
  99.                     sort = in.next().charAt(0); // Zeichen-Eingabe
  100.                 } while (sort != 'a' && sort != 'd');
  101.                 if (sort == 'a') { // Aufsteigend
  102.                     for (int i = 0; i < A.length; i++) {
  103.                         for (int j = 0; j < A.length; j++) {
  104.                             if (A[i] < A[j]) {
  105.                                 temp = A[i];
  106.                                 A[i] = A[j];
  107.                                 A[j] = temp;
  108.                             }
  109.                         }
  110.                     }
  111.                 } else if (sort == 'd') { // Absteigend
  112.                     for (int i = 0; i < A.length; i++) {
  113.                         for (int j = 0; j < A.length; j++) {
  114.                             if (A[i] > A[j]) {
  115.                                 temp = A[i];
  116.                                 A[i] = A[j];
  117.                                 A[j] = temp;
  118.                             }
  119.                         }
  120.                     }
  121.                 }
  122.                 break;
  123.             case "search":
  124.                 // Nach einem Wert suchen
  125.                 System.out.print("Give number to search for: ");
  126.                 num = in.nextInt();
  127.                 search = -1;
  128.                 for (int i = 0; i < A.length; i++) {
  129.                     if (A[i] == num) {
  130.                         search = i;
  131.                         break;
  132.                     }
  133.                 }
  134.                 if (search != -1)
  135.                     System.out.println("Found at pos: " + search);
  136.                 else
  137.                     System.out.println(num + " was not found!");
  138.                 break;
  139.             case "avg":
  140.                 // Durchschnitt berechnen
  141.                 if (sum == -1) { // wenn Summe noch nicht berechnet -> Fehler
  142.                     System.out.println("Choose “sum” first");
  143.                     break;
  144.                 }
  145.                 avg = (double) sum / A.length;
  146.                 System.out.println("avg of array is: " + avg);
  147.                 break;
  148.             case "exit":
  149.                 // Programm beenden
  150.                 in.close(); // Scanner schließen
  151.                 System.exit(0);
  152.             default:
  153.                 // Falsche Wahl
  154.                 System.out.println("Wrong choice! Give another choice!");
  155.             }
  156.  
  157.         } while (true);
  158.  
  159.     }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement