编写java程序,定义一个10个元素的整数数组,数组元素值为5,9,0,-3,90,4,23,-6,0,20,统计其中正数、负数和零的个数并输出
时间: 2024-05-10 17:20:45 浏览: 88
```java
public class Main {
public static void main(String[] args) {
int[] arr = {5, 9, 0, -3, 90, 4, 23, -6, 0, 20};
int positiveCount = 0;
int negativeCount = 0;
int zeroCount = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 0) {
positiveCount++;
} else if (arr[i] < 0) {
negativeCount++;
} else {
zeroCount++;
}
}
System.out.println("正数个数:" + positiveCount);
System.out.println("负数个数:" + negativeCount);
System.out.println("零的个数:" + zeroCount);
}
}
```
相关问题
编写JAVA程序,定义一个10个元素的整数数组,数组元素值为5,9,0,-3,90,4,23,-6,0,20,统计其中正数、负数和零的个数并输出
```java
public class Main {
public static void main(String[] args) {
int[] arr = {5, 9, 0, -3, 90, 4, 23, -6, 0, 20};
int positiveCount = 0; // 正数计数器
int negativeCount = 0; // 负数计数器
int zeroCount = 0; // 零计数器
// 遍历数组,统计正数、负数和零的个数
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 0) {
positiveCount++;
} else if (arr[i] < 0) {
negativeCount++;
} else {
zeroCount++;
}
}
// 输出结果
System.out.println("正数个数:" + positiveCount);
System.out.println("负数个数:" + negativeCount);
System.out.println("零的个数:" + zeroCount);
}
}
```
用java语言编写程序,定义一个10个元素的整数数组,数组元素值为5,9,0,-3,90,4,23,-6,0,20,统计其中正数、负数和零的个数并输出
```java
public class Main {
public static void main(String[] args) {
int[] arr = {5, 9, 0, -3, 90, 4, 23, -6, 0, 20};
int positiveCount = 0;
int negativeCount = 0;
int zeroCount = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 0) {
positiveCount++;
} else if (arr[i] < 0) {
negativeCount++;
} else {
zeroCount++;
}
}
System.out.println("正数个数:" + positiveCount);
System.out.println("负数个数:" + negativeCount);
System.out.println("零的个数:" + zeroCount);
}
}
```
输出结果:
```
正数个数:5
负数个数:2
零的个数:3
```
阅读全文
相关推荐
















