Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Bsp05 {
- public static void main(String[] args) {
- // Teil 1
- System.out.print("maximale Punktzahl: ");
- int max = SavitchIn.readInt();
- System.out.print("Anzahl der Ergebnisse: ");
- int howMany = SavitchIn.readInt();
- int[] array = createResults(howMany, max);
- System.out.print("Alle Ergebnisse: ");
- showResults(array);
- System.out.println();
- int maxPos = positionOfMax(array);
- System.out.println("Maximum: Wert " + array[maxPos] + " auf Position " + maxPos);
- int minPos = positionOfMin(array);
- System.out.println("Minimum: Wert " + array[minPos] + " auf Position " + minPos);
- System.out.println("Mittelwert: " + average(array));
- // Teil 2
- System.out.print("maximale Punktzahl: ");
- max = SavitchIn.readInt();
- System.out.print("Anzahl der Ergebnisse: ");
- howMany = SavitchIn.readInt();
- array = randomResults(howMany, max);
- System.out.print("Alle Ergebnisse: ");
- showResults(array);
- System.out.println();
- maxPos = positionOfMax(array);
- System.out.println("Maximum: Wert " + array[maxPos] + " auf Position " + maxPos);
- minPos = positionOfMin(array);
- System.out.println("Minimum: Wert " + array[minPos] + " auf Position " + minPos);
- System.out.println("Mittelwert: " + average(array));
- int[] grades = toGrades(array, max);
- System.out.print("Noten: ");
- showResults(grades);
- }
- public static int[] createResults(int howMany, int max) {
- int results [] = new int [howMany];
- System.out.println(howMany+" Ergebnisse eingeben (Bereich 0 bis "+max+")");
- for(int i = 0; i<results.length;i++ ) {
- int input;
- do {
- System.out.print("["+i+"]: ");
- input = SavitchIn.readLineInt();
- if (input <= max) {
- results[i] = input;
- }
- } while(input > max || input < 0);
- }
- return results;
- }
- // Print aus das Array
- public static void showResults (int results []) {
- for (int i = 0; i<results.length; i++) {
- if (i==results.length-1) {
- System.out.print(results[i]);
- }
- else {
- System.out.print(results[i] + ", ");
- }
- }
- }
- //Rechene aus Avarage
- public static double average (int[] results){
- int zahl = 0;
- for (int i = 0; i < results.length;i++) {
- zahl = zahl + results[i];
- }
- return (double) zahl / (double) results.length;
- }
- // Rechne Max aus
- public static int positionOfMax (int[] results){
- int max = -1;
- int index = -1;
- for (int i = 0; i < results.length;i++){
- if(results[i]>max || max == -1) {
- max = results[i];
- index = i;
- }
- }
- return index;
- }
- //Rechne MIN aus
- public static int positionOfMin (int[] results){
- int min = -1;
- int index = -1;
- for (int i = 0; i < results.length;i++){
- if(results[i]<min || min == -1) {
- min = results[i];
- index = i;
- }
- }
- return index;
- }
- //-------------------------------Teil 2. ----------------------------------------------
- //----------------------------------------------------------------------------------------
- // Gibt random Results ins Array rein
- public static int[] randomResults (int howMany, int max) {
- int results [] = new int [howMany];
- for(int i = 0; i<results.length;i++ ) {
- results[i] = PRNG.randomInt(max+1);
- }
- return results;
- }
- // umrechnen die Grades
- public static int[] toGrades (int[] results, int max) {
- int toGrades [] = new int [results.length];
- for(int i = 0; i<results.length;i++ ) {
- double ratio = (double) results[i] / (double) max;
- if(ratio<0.5) {
- toGrades[i] = 5;
- }
- else if(ratio<0.625) {
- toGrades[i] = 4;
- }
- else if(ratio<0.75) {
- toGrades[i] = 3;
- }
- else if(ratio<0.875) {
- toGrades[i] = 2;
- }
- else {
- toGrades[i] = 1;
- }
- }
- return toGrades;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement