Java中查找正数和负数数组元素的个数
在Java中,数组是一种非原始数据类型,它存储相同数据类型的数值。
根据题意,我们需要找到给定数组中正数、负数和零的个数。
大于零的数称为正数,小于零的数称为负数,否则为零。
让我们看看如何使用Java编程语言来实现。
一些示例
示例1
Suppose the original array is {2, 0, -1, 4, -6}
在上面的数组中,存在2个正数、2个负数和1个零。
示例2
Suppose the original array is {-12, -23, -11, 64}
在上面的数组中,存在1个正数和3个负数。
示例3
Suppose the original array is {11, 22, 0, 44, 0}
在上面的数组中,存在3个正数和2个零。
算法
步骤1 - 声明并初始化一个整数数组。使用3个变量来分别计数正数、负数和零元素。
步骤2 - 迭代数组的每个元素,并检查它是否大于零、小于零或等于零。相应地增加计数器的值。
步骤3 - 最后打印结果。
多种方法
我们提供了多种不同的方法来解决这个问题。
使用静态数组元素初始化
使用用户自定义方法
让我们逐一查看程序及其输出。
方法1:使用静态数组元素初始化
示例
在这种方法中,数组元素将在程序中初始化。然后根据算法检查正数、负数和零元素的总数。
import java.util.Arrays;
public class Main{
//main method
public static void main(String args[]){
//declared 3 integer variables and initialized all with zero
int positiveCount, negativeCount, zeroCount;
positiveCount=negativeCount=zeroCount=0;
//Declare and initialize the array elements
int arr[] = {4, 8, -2, 3, -1, 0, 7, 0, -9};
//get the length of the array
int size=arr.length;
// Print the array elements
System.out.println("Array elements are: "+Arrays.toString(arr));
//iterate each element of array
for(int i=0; i < arr.length; i++) {
//check positive number
if(arr[i] > 0)
positiveCount++;
//check negative number
else if(arr[i] < 0)
negativeCount++;
//check zero
else
zeroCount++;
}
//print the result
System.out.println("Count of positive numbers in array: "+positiveCount);
System.out.println("Count of negative numbers in array: "+negativeCount);
System.out.println("Count of zeroes in array: "+zeroCount);
}
}
输出
Array elements are: [4, 8, -2, 3, -1, 0, 7, 0, -9] Count of positive numbers in array: 4 Count of negative numbers in array: 3 Count of zeroes in array: 2
方法2:使用用户自定义方法
示例
在这种方法中,数组元素将在程序中初始化。然后调用用户自定义方法,并将数组作为参数传递。在方法内部,根据算法检查正数、负数和零元素的总数。
import java.util.Arrays;
public class Main{
//main method
public static void main(String args[]){
//Declare and initialize the array elements
int arr[] = {4, -2, 3, 7, 0, -9};
//calling the user defined method
findCount(arr);
}
//method to find frequency of postive, negative and zero elements
public static void findCount(int []arr){
//declared 3 integer variables and initialized all with zero
int positiveCount, negativeCount, zeroCount;
positiveCount=negativeCount=zeroCount=0;
//get the length of the array
int size=arr.length;
// Print the array elements
System.out.println("Array elements are: "+Arrays.toString(arr));
//iterate each element of array
for(int i=0; i < arr.length; i++) {
//check positive number
if(arr[i] > 0)
positiveCount++;
//check negative number
else if(arr[i] < 0)
negativeCount++;
//check zero
else
zeroCount++;
}
//print the result
System.out.println("Count of positive numbers in array: "+positiveCount);
System.out.println("Count of negative numbers in array: "+negativeCount);
System.out.println("Count of zeroes in array: "+zeroCount);
}
}
输出
Array elements are: [4, -2, 3, 7, 0, -9] Count of positive numbers in array: 3 Count of negative numbers in array: 2 Count of zeroes in array: 1
在本文中,我们探讨了如何在Java中查找数组中正数、负数和零的频率。
广告
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP