Java 程序:计算数组中的正数和负数

编写一个Java程序,使用For循环、While循环和函数来计算数组中的正数和负数,并附带示例。

Java 程序:使用 For 循环计算数组中的正数和负数

此程序允许用户输入大小和一维数组元素。接下来,它将使用 For 循环计算数组中正数和负数的总数。

import java.util.Scanner;
public class CountPositiveNegative1 {
private static Scanner sc;
public static void main(String[] args)
{
int Size, i;
int positiveCount = 0, negativeCount = 0;
sc = new Scanner(System.in);

System.out.print(" Please Enter Number of elements in an array : ");
Size = sc.nextInt();

int [] a = new int[Size];

System.out.print(" Please Enter " + Size + " elements of an Array : ");
for (i = 0; i < Size; i++)
{
a[i] = sc.nextInt();
}

for(i = 0; i < Size; i++)
{
if(a[i] >= 0)
{
positiveCount++;
}
else
{
negativeCount++;
}
}
System.out.println("\n Total Number of Positive Numbers in this Array = " + positiveCount);
System.out.println("\n Total Number of Negative Numbers in this Array = " + negativeCount);
}
}
Java Program to Count Positive and Negative Numbers in an Array 1

首先,我们声明用户指定大小的一维数组。接下来,我们使用 For 循环将用户输入的值存储为数组元素,例如 a[0], a[1], a[2], a[3], a[4]。

for (i = 0; i < Size; i++)
{
	a[i] = sc.nextInt();
}

在下一个Java行中,我们有另一个 for 循环来迭代每一个元素。在For 循环内,我们使用了If 语句

任何大于或等于 0 的数都是正数。If 语句将检查这一点。

  • 如果条件为 True,则为正数,Java 编译器将递增 positiveCount。
  • 如果条件为 False,则为负数。Javac 编译器将递增 negativeCount。
for(i = 0; i < Size; i++)
{
	if(a[i] >= 0)  {
		positiveCount++;
	}
	else  {
		negativeCount++;
	}
}

用户输入的数组值为 a[5] = {10, -15, 25, -19, -5}}

第一次迭代: for (i = 0; 0 < 5; 0++)
i 的值为 0,条件 (i < 5) 为 True。因此,它将开始执行 If 语句。

if(a[i] >= 0) => if(a[0] >= 0)
if(10 >= 0) – 条件为 True。因此,positiveCount 将变为 1

第二次迭代: for (i = 1; 1 < 5; 1++)
条件 (i < 5) 为 True。

if(a[1] >= 0)
if(-15 >= 0) – 条件为 False。因此,negativeCount 将变为 1

对剩余的迭代执行相同的操作,直到条件 (i < 5) 不满足为止。

Java 程序:使用 While 循环计算数组中的正数和负数

程序与上述程序相同,但这次我们使用While 循环来计算数组中的正数和负数。

import java.util.Scanner;
public class CountPositiveNegative2 {
private static Scanner sc;
public static void main(String[] args)
{
int Size, i = 0, j = 0;
int positiveCount = 0, negativeCount = 0;
sc = new Scanner(System.in);

System.out.print(" Please Enter Number of elements in an array : ");
Size = sc.nextInt();

int [] a = new int[Size];

System.out.print(" Please Enter " + Size + " elements of an Array : ");
while (i < Size)
{
a[i] = sc.nextInt();
i++;
}

while (j < Size)
{
if(a[j] >= 0)
{
positiveCount++;
}
else
{
negativeCount++;
}
j++;
}
System.out.println("\n Total Number of Positive Numbers in this Array = " + positiveCount);
System.out.println("\n Total Number of Negative Numbers in this Array = " + negativeCount);
}
}

使用 While 循环计算正数和负数数组项的输出

 Please Enter Number of elements in an array : 10
 Please Enter 10 elements of an Array  : 4 -8 -12 15 -17 -25 105 110 -89 77

 Total Number of Positive Numbers in this Array = 5

 Total Number of Negative Numbers in this Array = 5

Java 程序:使用方法计算数组中的正数和负数

程序与第一个示例相同。但这次我们创建了一个单独的方法来计算正数,并创建了另一个方法来计算负数。

import java.util.Scanner;
public class CountPositiveNegative3 {
private static Scanner sc;
public static void main(String[] args)
{
int Size, i;
int positiveCount = 0, negativeCount = 0;
sc = new Scanner(System.in);

System.out.print(" Please Enter Number of elements in an array : ");
Size = sc.nextInt();

int [] a = new int[Size];

System.out.print(" Please Enter " + Size + " elements of an Array : ");
for (i = 0; i < Size; i++)
{
a[i] = sc.nextInt();
}

positiveCount = CountPositive(a, Size);
negativeCount = CountNegative(a, Size);
System.out.println("\n Total Number of Positive Numbers in this Array = " + positiveCount);
System.out.println(" Total Number of Negative Numbers in this Array = " + negativeCount);

}
public static int CountPositive(int [] a, int Size)
{
int i, positiveCount = 0;
System.out.print("\n List of Positive Numbers in this Array are :");
for(i = 0; i < Size; i++)
{
if(a[i] >= 0)
{
System.out.print(a[i] +" ");
positiveCount++;
}
}
return positiveCount;
}
public static int CountNegative(int [] a, int Size)
{
int i, negativeCount = 0;
System.out.print("\n List of Negative Numbers in this Array are :");
for(i = 0; i < Size; i++)
{
if(a[i] < 0)
{
System.out.print(a[i] + " ");
negativeCount++;
}
}
return negativeCount;
}
}
 Please Enter Number of elements in an array : 10
 Please Enter 10 elements of an Array  : 10 -25 -14 8 -19 -78 105 -77 -11 4

 List of Positive Numbers in this Array are :10 8 105 4 
 List of Negative Numbers in this Array are :-25 -14 -19 -78 -77 -11 
 Total Number of Positive Numbers in this Array = 4
 Total Number of Negative Numbers in this Array = 6