Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class Competitors {
  5. public static void main(String[] args) {
  6.  
  7. System.out.println("Enter the number of competitors ");
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10. int numberOfCompetitors = scanner.nextInt();
  11. if(numberOfCompetitors <= 1 ){
  12. System.out.println("Incorrect value");
  13. System.exit(0);
  14. }
  15.  
  16. int[] competitors = new int[numberOfCompetitors];
  17.  
  18. //Enter score for each competitor
  19. for(int i = 0; i < competitors.length; i++){
  20. System.out.println("Enter the time of the " + (i + 1) + "'s" + " competitor in sec.");
  21. competitors[i] = scanner.nextInt();
  22.  
  23. if(competitors[i] <= 0 ){
  24. System.out.println("Incorrect value");
  25. System.exit(0);
  26. }
  27. }
  28.  
  29. //Sorting the array
  30. Arrays.sort(competitors);
  31.  
  32. //calculate the amount of time
  33. int sum = 0;
  34. for(int i = 0; i < competitors.length; i++){
  35. sum += competitors[i];
  36. }
  37.  
  38. //avg
  39. double average = sum / competitors.length;
  40.  
  41. //calculate the standard deviation
  42. int round = 0;
  43. double x = 0;
  44. double standardDeviation;
  45. while(true){
  46. if(round == (competitors.length)){
  47. standardDeviation = Math.sqrt(x / competitors.length);
  48. break;
  49. }
  50. x = x + Math.pow(competitors[round] - average,2);
  51. round++;
  52. }
  53.  
  54.  
  55. System.out.println("All results: " + Arrays.toString(competitors));
  56. System.out.printf("The fastest result: %d sec %n", competitors[0]);
  57. System.out.printf("The slowest result: %d sec %n",competitors[competitors.length - 1]);
  58. System.out.printf("Avg: %.1f%n",(double)(sum / competitors.length));
  59. System.out.printf("Standard deviation: %.2f sec %n",standardDeviation);
  60.  
  61.  
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement