T9.java 790 B

1234567891011121314151617181920212223242526272829303132
  1. package com.loveCoding.homework.j20250517_method;
  2. /**
  3. * @author WanJl
  4. * @version 1.0
  5. * @title T9
  6. * @description **9. 数字频率统计**
  7. * 要求:统计数组中每个数字出现的次数(假设数字范围0-9)
  8. * 示例输入:`{2,5,3,2,8,2}`
  9. * 示例输出:
  10. * `2出现3次`
  11. * `3出现1次`
  12. * `5出现1次`
  13. * `8出现1次`
  14. * @create 2025/5/24
  15. */
  16. public class T9 {
  17. public static void main(String[] args) {
  18. int[] arr = {2, 5, 3, 2, 8, 2, 2};
  19. for (int i = 0; i < arr.length; i++) {
  20. int count = 0;
  21. for (int j = i; j < arr.length; j++) {
  22. if (arr[i] == arr[j]) {
  23. count++;
  24. }
  25. }
  26. System.out.println(arr[i] + "出现" + count + "次");
  27. }
  28. }
  29. }