12345678910111213141516171819202122232425262728 |
- package com.loveCoding.homework.j20250517;
- /**
- * @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 main(String[] args) {
- int[] arr = {5, -2, 0, 3, -1, 0, 7};
- 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+"个");
- }
- }
|