Advertisement
kocev

StandardDeviation

Feb 21st, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 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.         //Check if scores was given correctly.
  28.         while (n != scores.length || n != scoresLine.length || !isDigit) {
  29.             System.out.println("Your input is wrong. Try again.");
  30.             scoresLine = scanner.nextLine().split(",".trim());
  31.         }
  32.  
  33.         System.out.printf("The fastest is %.2f%nThe slowest is %.2f%nThe average is %.2f%nThe standard deviation is %.2f%n",
  34.                 theFastest(scores), theSlowest(scores), average(scores), standardDeviation(scores));
  35.  
  36.     }
  37.    
  38.     public static double theFastest(double[] arr) {
  39.         double fastest = arr[0];
  40.         for (int i = 0; i < arr.length; i++) {
  41.             if (arr[i] > fastest) {
  42.                 fastest = arr[i];
  43.             }
  44.         }
  45.         return fastest;
  46.     }
  47.  
  48.     public static double theSlowest(double[] arr) {
  49.         double slowest = arr[arr.length - 1];
  50.         for (int i = 0; i < arr.length; i++) {
  51.             if (arr[i] < slowest) {
  52.                 slowest = arr[i];
  53.             }
  54.         }
  55.         return slowest;
  56.     }
  57.  
  58.     public static double average(double[] arr) {
  59.         double sum = 0;
  60.         double average = 1;
  61.         for (int i = 0; i < arr.length; i++) {
  62.             sum += arr[i];
  63.             average = sum / arr.length;
  64.         }
  65.         return average;
  66.     }
  67.  
  68.     public static double standardDeviation(double[] arr) {
  69.         double sd1 = 0;
  70.         double sd = 0;
  71.         for (int i = 0; i < arr.length; i++) {
  72.             sd1 += Math.pow(arr[i] - average(arr), 2);
  73.             sd = Math.sqrt(sd1 / arr.length);
  74.         }
  75.         return sd;
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement