Java 程序:查找数组中奇数的和

编写一个 Java 程序,使用 For 循环、While 循环和函数查找数组中奇数的和,并附有示例。

Java 程序:使用 For 循环查找数组中奇数的和

此 Java 程序允许用户输入数组的大小和元素。接下来,它将使用 For 循环查找数组中奇数的和。

// Java Program to find Sum of Odd Numbers in an Array using For Loop
import java.util.Scanner;

public class SumOfOdds1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i, OddSum = 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] % 2 != 0)
			{
				OddSum = OddSum + a[i]; 
			}
		}		
		System.out.println("\n The Sum of Odd Numbers in this Array = " + OddSum);
	}
}
Java Program to find Sum of Odd Numbers in an Array 1

首先,我们使用 for 循环迭代每个元素。在循环内,我们使用 Java If 语句 检查该数字是否不能被 2 整除(即是否为奇数)。

for(i = 0; i < Size; i++)
{
	if(a[i] % 2 != 0)
	{
		OddSum = OddSum + a[i]; 
	}
}

用户插入的 数组 值为 a[5] = {10, 25, 35, 60, 77}},OddSum = 0

第一次迭代: for (i = 0; 0 < 5; 0++)
i 的值为 0,条件 (i < 5) 为 True。因此,Java 编译器将进入 For 循环 内的 If 语句。

if(a[i] % 2 != 0) => if(a[0] % 2 != 0)
if(10 % 2 != 0) – 条件为 False。

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

if(a[1] % 2 != 0)
if(25 % 2 != 0) – 条件为 True。
OddSum = OddSum + a[i]
OddSum = 0 + 25 = 25

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

if(a[2] % 2 != 0)
if(35 % 2 != 0) – 条件为 True。
OddSum = 25 + 35 = 60

对剩余的迭代执行相同的操作,直到条件 (i < 5) 失败。

Java 程序:使用 While 循环查找数组中奇数的和

此 Java 程序与上面相同,但这次我们使用 While 循环 来计算数组中奇数的和。

// Java Program to find Sum of Odd Numbers in an Array using While Loop
import java.util.Scanner;

public class SumOfOdds2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i = 0, j = 0, OddSum = 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] % 2 != 0)
			{
				OddSum = OddSum + a[j]; 
			}
			j++;
		}		
		System.out.println("\n The Sum of Odd Numbers in this Array = " + OddSum);
	}
}

Java 使用 While 循环计算奇数数组和的输出

 Please Enter Number of elements in an array : 10
 Please Enter 10 elements of an Array  : 15 25 98 75 62 14 12 13 22 77

 The Sum of Odd Numbers in this Array = 205

使用方法查找数组中奇数和的程序

Java 程序 与第一个示例相同。但是,我们将计算奇数和的逻辑分离到了 Method 中。

// Java Program to find Sum of Even Numbers in an Array using Functions
import java.util.Scanner;

public class SumOfOdds3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i, OddSum = 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();
		}   
		OddSum = SumOfOddsinArray(a, Size);
		System.out.println("\n The Sum of Odd Numbers in this Array = " + OddSum);
	}
	public static int SumOfOddsinArray(int[] a, int Size)
	{
		int i, OddSum = 0;		
		for(i = 0; i < Size; i++)
		{
			if(a[i] % 2 != 0)
			{
				OddSum = OddSum + a[i]; 
			}
		}	
		return OddSum;
	}
}

Java 使用函数计算数组奇数和的输出

 Please Enter Number of elements in an array : 8
 Please Enter 8 elements of an Array  : 5 19 35 146 89 17 28 105

 The Sum of Odd Numbers in this Array = 270