Advertisement
myrdok123

03. Histogram

May 21st, 2023
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. package L04_ForLoop;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P03_Histogram {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.  
  10.         int n = Integer.parseInt(scanner.nextLine());
  11.  
  12.         //създаваме променливи за броя числа в конкретен диапазон
  13.  
  14.         int countP1 = 0;
  15.         int countP2 = 0;
  16.         int countP3 = 0;
  17.         int countP4 = 0;
  18.         int countP5 = 0;
  19.  
  20.         //правим цикъл и прочитаме n на брой числа
  21.  
  22.  
  23.  
  24.         for (int i = 1; i <= n ; i++) {
  25.  
  26.             int currentNum = Integer.parseInt(scanner.nextLine());
  27.  
  28.             //проверяваме в кой диапазон попада текущото число
  29.             if (currentNum < 200){
  30.                 countP1++;
  31.             } else if (currentNum < 400) {
  32.                 countP2++;
  33.             } else if (currentNum < 600) {
  34.                 countP3++;
  35.             } else if (currentNum < 800) {
  36.                 countP4++;
  37.             }else {
  38.                 countP5++;
  39.             }
  40.  
  41.         }
  42.  
  43.         double p1Percent = (countP1 * 1.0 / n) * 100;
  44.         double p2Percent = (countP2 * 1.0 / n) * 100;
  45.         double p3Percent = (countP3 * 1.0 / n) * 100;
  46.         double p4Percent = (countP4 * 1.0 / n) * 100;
  47.         double p5Percent = (countP5 * 1.0 / n) * 100;
  48.  
  49.  
  50.         System.out.printf("%.2f%%%n", p1Percent);
  51.         System.out.printf("%.2f%%%n", p2Percent);
  52.         System.out.printf("%.2f%%%n", p3Percent);
  53.         System.out.printf("%.2f%%%n", p4Percent);
  54.         System.out.printf("%.2f%%%n", p5Percent);
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement