binibiningtinamoran

ArrayOfCounters

Jun 29th, 2021
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ArrayOfCounters {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         int input;
  8.         int[] frequencyCounter = new int[51];
  9.        
  10.         do {
  11.             System.out.print("Enter an integer between 0 and 50, inclusive: ");
  12.             input = scanner.nextInt();
  13.             if (input > -1 && input < 51) {
  14.                 frequencyCounter[input]++;
  15.             }
  16.         } while (input > -1 && input < 51);
  17.  
  18.         for (int i=0; i<frequencyCounter.length; i++) {
  19.             if (frequencyCounter[i] > 0) {
  20.                 System.out.println(i + " : " + frequencyCounter[i]);
  21.             }
  22.         }
  23.     }
  24. }
  25.  
Advertisement
Add Comment
Please, Sign In to add comment