您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

在Java中找到正数和负数数组元素的数量

2025/12/13 0:52:00发布36次查看
在 java 中,数组是一种非原始数据类型,它存储类似数据类型的值。
根据问题陈述,我们必须找到给定数组中存在的正数、负数和零的数量。
任何大于零的数字都被称为正数,如果数字小于零,则为负数,否则为零。
让我们看看如何使用java编程语言来实现它。
展示一些实例给你看实例1suppose the original array is {2, 0, -1, 4, -6}
在上面的数组中,有2个正数,2个负数和1个零。
实例2suppose the original array is {-12, -23, -11, 64}
在上面的数组中,存在1个正数和3个负数。
实例3suppose the original array is {11, 22, 0, 44, 0}
在上面的数组中,存在 3 个正数和 2 个零。
算法步骤 1 - 声明并初始化一个整数数组。使用3个变量来记录正数、负数和零元素的数量。
第二步 - 迭代数组的每个元素,并检查它是否大于零,小于零或等于零。分别增加计数值。
步骤 3 - 最后打印结果。
多种方法我们通过不同的方式提供了解决方案。
通过使用静态初始化数组元素
通过使用用户定义的方法
让我们逐个查看程序及其输出。
方法一:通过使用数组元素的静态初始化示例在这种方法中,数组元素将在程序中初始化。然后根据算法检查正、负和零元素的总数。
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: 4count of negative numbers in array: 3count 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: 3count of negative numbers in array: 2count of zeroes in array: 1
在本文中,我们探讨了如何在 java 中查找数组中正数、负数和零的出现频率。
以上就是在java中找到正数和负数数组元素的数量的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product