Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package steemit; //das so gennante Packet des Projekts wo der Code drinne ist
- import java.util.Scanner; // Bibliothek für das Scanner Objekt
- public class clike { // "clike" Klasse. Die .java Datei sollte "clike.java" heißen
- public static void main(String[] args) { // die Main-Funktion
- // Variablen Deklarieren
- Scanner in = new Scanner(System.in); // Scanner Objekt
- int[] A; // Ganzzahl-Feld deklarieren
- int size; // Feldgrößen-Variable
- int a, b; // Werte für "fill"
- int sum = -1, max, min; // Summe, Maximal und Minimal Wert
- // sum ist "-1" so das wir kontrollieren ob es bereits ausfgeführt wurde
- int search, num, temp; // Eingabe vom Benutzer
- double avg = -1; // Durchschnitt auch "-1"
- char sort; // für die Sortierungsweise
- String choice; // Zeichenkette für Switch-Case
- // Feldgröße als Eingabe nehmen
- do {
- System.out.print("Give size for array: "); // Ausgabe
- size = in.nextInt(); // Ganzzahl Eingabe
- // Kontrollieren ob der Wert in 1...100 ist.
- if (size < 1 || size > 100)
- // "Fehler" Meldung
- System.out.println("The size must be in the range of 1...100");
- } while (size < 1 || size > 100);
- A = new int[size]; // Feld Objekt erstellen-zuweisen
- do { // Unendlicher Loop
- // Menü Ausgabe
- System.out.println("“fill”: fill Array with rand numbers");
- System.out.println("“sum” : sum Array");
- System.out.println("“max” : find max");
- System.out.println("“min” : find min");
- System.out.println("“sort” : sort Array");
- System.out.println("“search” : search number");
- System.out.println("“avg” : average");
- System.out.println("“Q” : implementation of question Q");
- System.out.println("“exit” : Exit");
- // Auswahl-Eingabe
- System.out.print("choice: ");
- choice = in.next(); // Zeichenketten-Eingabe
- // switch-case von einem String! Das konnte man bei C nicht machen!
- switch (choice) {
- case "fill":
- // a und b Grenzen Eingabe
- do {
- System.out.println("Give 2 ints to fill array with random numbers between them");
- System.out.print("a: ");
- a = in.nextInt();
- System.out.print("b: ");
- b = in.nextInt();
- // kontrollieren ob a < b ist
- if (!(a < b))
- System.out.println("a must be less then b");
- } while (!(a < b));
- // Feld mit Ganzzahlen in [a, b] füllen
- for (int i = 0; i < A.length; i++) {
- // Zufällige Nummer in a...b
- A[i] = a + (int) ((b - a + 1) * Math.random());
- }
- break;
- case "sum":
- // Summe berechnen
- sum = 0;
- for (int i = 0; i < A.length; i++) {
- sum += A[i];
- }
- System.out.println("sum of array is: " + sum);
- break;
- case "max":
- // Maximal-Wert finden
- max = 0;
- for (int i = 1; i < A.length; i++) {
- if (A[i] > A[max])
- max = i;
- }
- System.out.println("max is " + A[max]);
- break;
- case "min":
- // Minimal-Wert finden
- min = 0;
- for (int i = 1; i < A.length; i++) {
- if (A[i] < A[min])
- min = i;
- }
- System.out.println("min is " + A[min]);
- break;
- case "sort":sorting array
- // Feld Sortieren
- // Sortier-Weise Eingeben
- System.out.println("Do you want ascending (a) or descending (d) order?");
- do {
- System.out.print("Choice: ");
- sort = in.next().charAt(0); // Zeichen-Eingabe
- } while (sort != 'a' && sort != 'd');
- if (sort == 'a') { // Aufsteigend
- for (int i = 0; i < A.length; i++) {
- for (int j = 0; j < A.length; j++) {
- if (A[i] < A[j]) {
- temp = A[i];
- A[i] = A[j];
- A[j] = temp;
- }
- }
- }
- } else if (sort == 'd') { // Absteigend
- for (int i = 0; i < A.length; i++) {
- for (int j = 0; j < A.length; j++) {
- if (A[i] > A[j]) {
- temp = A[i];
- A[i] = A[j];
- A[j] = temp;
- }
- }
- }
- }
- break;
- case "search":
- // Nach einem Wert suchen
- System.out.print("Give number to search for: ");
- num = in.nextInt();
- search = -1;
- for (int i = 0; i < A.length; i++) {
- if (A[i] == num) {
- search = i;
- break;
- }
- }
- if (search != -1)
- System.out.println("Found at pos: " + search);
- else
- System.out.println(num + " was not found!");
- break;
- case "avg":
- // Durchschnitt berechnen
- if (sum == -1) { // wenn Summe noch nicht berechnet -> Fehler
- System.out.println("Choose “sum” first");
- break;
- }
- avg = (double) sum / A.length;
- System.out.println("avg of array is: " + avg);
- break;
- case "exit":
- // Programm beenden
- in.close(); // Scanner schließen
- System.exit(0);
- default:
- // Falsche Wahl
- System.out.println("Wrong choice! Give another choice!");
- }
- } while (true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement