123456789101112131415161718192021222324252627282930313233 |
- package com.loveCoding.homework.j20250517_method;
- /**
- * @author WanJl
- * @version 1.0
- * @title T1
- * @description 1. 统计数组元素类型
- * 要求:统计数组中正数、负数和零的个数
- * 示例输入:`{5, -2, 0, 3, -1, 0, 7}`
- * 示例输出:正数3个,负数2个,零2个
- * @create 2025/5/24
- */
- public class T1 {
- //有参数但是无返回值的方法
- public static void method(int[] arr){
- int a=0,b=0,c=0;
- for (int i = 0; i < arr.length; i++) {
- if (arr[i] > 0) {
- a++;
- } else if (arr[i] < 0) {
- b++;
- } else{
- c++;
- }
- }
- System.out.println("正数"+a+"个,负数"+b+"个,零"+c+"个");
- }
- public static void main(String[] args) {
- int[] arr = {5, -2, 0, 3, -1, 0, 7};
- method(arr);
- }
- }
|