Advertisement
kocev

StandardDeviation2

Feb 26th, 2020
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class StandartDeviation {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         //First we enter the number of players and check if the input is correct.
  8.         System.out.println("Enter number of players: ");
  9.         String number = scanner.nextLine();
  10.         while (number.matches("\\D+")) {
  11.             System.out.println("Number must be integer. Try again.");
  12.             number = scanner.nextLine();
  13.         }
  14.         int n = Integer.parseInt(number);
  15.         //Then we create the score array and check it.
  16.         double[] scores = new double[n];
  17.         boolean isDigit = true;
  18.         System.out.println("Enter scores separated by ','");
  19.         String[] scoresLine = scanner.nextLine().split(",".trim());
  20.         for (int i = 0; i < n; i++) {
  21.             if (!scoresLine[i].matches("\\d+")) {
  22.                 System.out.printf("The score %d is entered incorrectly.%n", i + 1);
  23.                 isDigit = false;
  24.                 break;
  25.             } else scores[i] = Double.parseDouble(scoresLine[i]);
  26.         }
  27.         while (n != scores.length || n != scoresLine.length || !isDigit) {
  28.             System.out.println("Your input is wrong. Try again.");
  29.             scoresLine = scanner.nextLine().split(",".trim());
  30.         }
  31.  
  32.         System.out.printf("The fastest is %.2f%nThe slowest is %.2f%nThe average is %.2f%nThe standard deviation is %.2f%n",
  33.                 theFastest(scores), theSlowest(scores), average(scores), standardDeviation(scores));
  34.  
  35.     }
  36.  
  37.     public static double theFastest(double... arr) {
  38.         double fastest = arr[0];
  39.         for (int i = 0; i < arr.length; i++) {
  40.             if (arr[i] > fastest) {
  41.                 fastest = arr[i];
  42.             }
  43.         }
  44.         return fastest;
  45.     }
  46.  
  47.     public static double theSlowest(double... arr) {
  48.         double slowest = arr[arr.length - 1];
  49.         for (int i = 0; i < arr.length; i++) {
  50.             if (arr[i] < slowest) {
  51.                 slowest = arr[i];
  52.             }
  53.         }
  54.         return slowest;
  55.     }
  56.  
  57.     public static double average(double... arr) {
  58.         double sum = 0;
  59.         double average = 1;
  60.         for (int i = 0; i < arr.length; i++) {
  61.             sum += arr[i];
  62.             average = sum / arr.length;
  63.         }
  64.         return average;
  65.     }
  66.  
  67.     public static double standardDeviation(double... arr) {
  68.         double sd1 = 0;
  69.         double sd = 0;
  70.         for (int i = 0; i < arr.length; i++) {
  71.             sd1 += Math.pow(arr[i] - average(arr), 2);
  72.             sd = Math.sqrt(sd1 / arr.length);
  73.         }
  74.         return sd;
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement