T1.java 876 B

123456789101112131415161718192021222324252627282930313233
  1. package com.loveCoding.homework.j20250517_method;
  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. //有参数但是无返回值的方法
  14. public static void method(int[] arr){
  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. public static void main(String[] args) {
  28. int[] arr = {5, -2, 0, 3, -1, 0, 7};
  29. method(arr);
  30. }
  31. }