T1.java 756 B

12345678910111213141516171819202122232425262728
  1. package com.loveCoding.homework.j20250517;
  2. /**
  3. * @author WanJl
  4. * @version 1.0
  5. * @title T1
  6. * @description 1. 统计数组元素类型
  7. * 要求:统计数组中正数、负数和零的个数
  8. * 示例输入:`{5, -2, 0, 3, -1, 0, 7}`
  9. * 示例输出:正数3个,负数2个,零2个
  10. * @create 2025/5/24
  11. */
  12. public class T1 {
  13. public static void main(String[] args) {
  14. int[] arr = {5, -2, 0, 3, -1, 0, 7};
  15. int a=0,b=0,c=0;
  16. for (int i = 0; i < arr.length; i++) {
  17. if (arr[i] > 0) {
  18. a++;
  19. } else if (arr[i] < 0) {
  20. b++;
  21. } else{
  22. c++;
  23. }
  24. }
  25. System.out.println("正数"+a+"个,负数"+b+"个,零"+c+"个");
  26. }
  27. }