Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Random;
- public class CountOccurrences {
- public static void main(String[] args) {
- int[] arr = new int[1000];
- Random rand = new Random();
- int num, oneCount = 0, twoCount = 0, threeCount = 0,
- fourCount = 0, fiveCount = 0, sixCount = 0;
- for (int i = 0; i < arr.length; i++) {
- num = rand.nextInt(7);
- arr[i] = num;
- if (num == 1) { // or arr[i] == 1
- oneCount += 1;
- } else if (num == 2) {
- twoCount += 1;
- } else if (num == 3) {
- threeCount += 1;
- } else if (num == 4) {
- fourCount += 1;
- } else if (num == 5) {
- fiveCount += 1;
- } else {
- sixCount += 1;
- }
- }
- System.out.println();
- // Sort the array and print contents
- Arrays.sort(arr);
- for (int number : arr) {
- System.out.println(number);
- }
- System.out.println();
- System.out.println("Count of 1: " + oneCount);
- System.out.println("Count of 2: " + twoCount);
- System.out.println("Count of 3: " + threeCount);
- System.out.println("Count of 4: " + fourCount);
- System.out.println("Count of 5: " + fiveCount);
- System.out.println("Count of 6: " + sixCount);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment