Java 程序将正数和负数放入单独的数组

编写一个 Java 程序,将正数和负数放入单独的数组,并附带示例。或者如何编写一个 Java 程序来查找并将正负项放入单独的数组。

在这个 Java 正负数单独数组的示例中,我们使用 while 循环来迭代 count_PosNegArr 数组。我们在循环中使用 If Else 语句将正项存储在 pos_Arr 中,将负项存储在 neg_Arr 中。之后,我们打印出来。

package ArrayPrograms;

public class SeparatePositiveNegativeArrayItems0 {
	
	public static void main(String[] args) {
		
		int i = 0, pos_count = 0, neg_count = 0;
		int[] count_PosNegArr = {15, -4, 11, -8, -13, 2, 16, -11, 9, -5, 18, 60};
		int[] pos_Arr = new int[10];
		int[] neg_Arr = new int[10];
		
		while(i < count_PosNegArr.length) 
		{
			if(count_PosNegArr[i] >= 0) {
				pos_Arr[pos_count] = count_PosNegArr[i];
				pos_count++;
			}
			else {
				neg_Arr[neg_count] = count_PosNegArr[i];
				neg_count++;
			}
			i++;
		}
		System.out.println("\nThe Total Number of Positive Items = " + pos_count);
		System.out.print("The List of Items in Positive Array = ");
		PrintArrayElement(pos_Arr, pos_count);
		
		System.out.println("\n\nThe Total Number of Negative Items = " + neg_count);
		System.out.print("The List of Items in Negative Array = ");
		PrintArrayElement(neg_Arr, neg_count);
	}
	public static void PrintArrayElement(int[] arr, int size) {
		int i;
		
		for(i = 0; i < size; i++) 
		{
			System.out.format("%d ", arr[i]);
		}
	}
}
The Total Number of Positive Items = 7
The List of Items in Positive Array = 15 11 2 16 9 18 60 

The Total Number of Negative Items = 5
The List of Items in Negative Array = -4 -8 -13 -11 -5 

Java 程序使用 For 循环将正数和负数放入单独的数组

在此 Java 示例中,我们将 while 循环替换为 for 循环,它还允许用户输入 count_PosNegArr 数组的大小和项。

package ArrayPrograms;

import java.util.Scanner;

public class SeparatePositiveNegativeArrayItems1 {
	private static Scanner sc;
	public static void main(String[] args) {
		
		int i, Size, pos_count = 0, neg_count = 0;
		
		sc = new Scanner(System.in);
		
		System.out.print("\nPlease Enter the PUT POS & NEG Array size  : ");
		Size = sc.nextInt();
		
		int[] pos_Arr = new int[Size];
		int[] neg_Arr = new int[Size];
		int[] count_PosNegArr = new int[Size];
		
		System.out.format("\nEnter PUT POS & NEG Array %d elements : ", Size);
		for(i = 0; i < Size; i++) 
		{
			count_PosNegArr[i] = sc.nextInt();
		}
		
		for(i = 0; i < count_PosNegArr.length; i++) 
		{
			if(count_PosNegArr[i] >= 0) {
				pos_Arr[pos_count] = count_PosNegArr[i];
				pos_count++;
			}
			else {
				neg_Arr[neg_count] = count_PosNegArr[i];
				neg_count++;
			}
		}
		System.out.println("\nThe Total Number of Positive Items = " + pos_count);
		System.out.print("The List of Items in Positive Array = ");
		PrintArrayElement(pos_Arr, pos_count);
		
		System.out.println("\n\nThe Total Number of Negative Items = " + neg_count);
		System.out.print("The List of Items in Negative Array = ");
		PrintArrayElement(neg_Arr, neg_count);
	}
	public static void PrintArrayElement(int[] arr, int size) {
		int i;
		
		for(i = 0; i < size; i++) 
		{
			System.out.format("%d ", arr[i]);
		}
	}
}
Java Program to Put Positive and Negative Numbers in Separate Array

在这个 Java 正负数单独数组的示例中,我们创建了一个单独的 void 函数 PutPositiveItems,用于将正项放入 pos_Arr。以及另一个函数 PutNegativeItems,用于将负项放入 neg_arr。

package ArrayPrograms;

import java.util.Scanner;

public class SeparatePositiveNegativeArrayItem2 {
	private static Scanner sc;
	public static void main(String[] args) {
		
		int i, Size;
		
		sc = new Scanner(System.in);
		
		System.out.print("\nPlease Enter the PUT POS & NEG Array size  : ");
		Size = sc.nextInt();
		
		int[] count_PosNegArr = new int[Size];
		
		System.out.format("\nEnter PUT POS & NEG Array %d elements : ", Size);
		for(i = 0; i < Size; i++) 
		{
			count_PosNegArr[i] = sc.nextInt();
		}
		PutPositiveItems(count_PosNegArr,Size);
		PutNegativeItems(count_PosNegArr, Size );

	}
	public static void PutPositiveItems(int[] count_PosNegArr, int size ) {
		int i, pos_count = 0;
		int[] pos_Arr = new int[size];

		for(i = 0; i < count_PosNegArr.length; i++) 
		{
			if(count_PosNegArr[i] >= 0) {
				pos_Arr[pos_count] = count_PosNegArr[i];
				pos_count++;
			}
		}
		System.out.println("\nThe Total Number of Positive Items = " + pos_count);
		System.out.print("The List of Items in Positive Array = ");
		PrintArrayElement(pos_Arr, pos_count);
	}
	public static void PutNegativeItems(int[] count_PosNegArr, int size ) {
		int i, neg_count = 0;
		int[] neg_Arr = new int[size];
		
		for(i = 0; i < count_PosNegArr.length; i++) 
		{
			if(count_PosNegArr[i] < 0) {

				neg_Arr[neg_count] = count_PosNegArr[i];
				neg_count++;
			}
		}
		System.out.println("\n\nThe Total Number of Negative Items = " + neg_count);
		System.out.print("The List of Items in Negative Array = ");
		PrintArrayElement(neg_Arr, neg_count);
	}
	public static void PrintArrayElement(int[] arr, int size) {
		int i;
		
		for(i = 0; i < size; i++) 
		{
			System.out.format("%d ", arr[i]);
		}
	}
}
Please Enter the PUT POS & NEG Array size  : 11

Enter PUT POS & NEG Array 11 elements : 19 -33 -55 29 -66 -77 39 59 -88 -99 79

The Total Number of Positive Items = 5
The List of Items in Positive Array = 19 29 39 59 79 

The Total Number of Negative Items = 6
The List of Items in Negative Array = -33 -55 -66 -77 -88 -99