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

WBOY
WBOY 转载
2023-08-29 14:25:05 734浏览

在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个变量来记录正数、负数和零元素的数量。

  • 第二步 - 迭代数组的每个元素,并检查它是否大于零,小于零或等于零。分别增加计数值。

  • 步骤 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: 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 中查找数组中正数、负数和零的出现频率。

以上就是在Java中找到正数和负数数组元素的数量的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除