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中查詢陣列中正數、負數和零的頻率。

更新於:2023年1月5日

7K+ 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告