编写java程序,定义一个10个元素的整数数组,数组元素值为5,9,0,-3,90,4,23,-6,0,20,统计其中正数、负数和零的个数并输出
时间: 2024-05-10 14:20:45 浏览: 84
```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 ArrayDemo {
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
```
阅读全文