binibiningtinamoran

CountOccurrences

Nov 20th, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Random;
  3.  
  4. public class CountOccurrences {
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         int[] arr = new int[1000];
  9.         Random rand = new Random();
  10.         int num, oneCount = 0, twoCount = 0, threeCount = 0,
  11.                 fourCount = 0, fiveCount = 0, sixCount = 0;
  12.  
  13.         for (int i = 0; i < arr.length; i++) {
  14.             num = rand.nextInt(7);
  15.             arr[i] = num;
  16.             if (num == 1) { // or arr[i] == 1
  17.                 oneCount += 1;
  18.             } else if (num == 2) {
  19.                 twoCount += 1;
  20.             } else if (num == 3) {
  21.                 threeCount += 1;
  22.             } else if (num == 4) {
  23.                 fourCount += 1;
  24.             } else if (num == 5) {
  25.                 fiveCount += 1;
  26.             } else {
  27.                 sixCount += 1;
  28.             }
  29.         }
  30.         System.out.println();
  31.         // Sort the array and print contents
  32.         Arrays.sort(arr);
  33.         for (int number : arr) {
  34.             System.out.println(number);
  35.         }
  36.         System.out.println();
  37.         System.out.println("Count of 1: " + oneCount);
  38.         System.out.println("Count of 2: " + twoCount);
  39.         System.out.println("Count of 3: " + threeCount);
  40.         System.out.println("Count of 4: " + fourCount);
  41.         System.out.println("Count of 5: " + fiveCount);
  42.         System.out.println("Count of 6: " + sixCount);
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment