编写一个Java程序,使用For循环、While循环和函数通过示例查找数组中奇数和偶数的总和。
Java 程序使用 For 循环查找数组中奇数和偶数的总和
此Java程序允许用户输入大小和数组元素。接下来,它将使用For循环查找此数组中的偶数总和和奇数总和。
import java.util.Scanner;
public class SumOfEvenOdds1 {
private static Scanner sc;
public static void main(String[] args)
{
int Size, i, EvenSum = 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 : ");
for (i = 0; i < Size; i++)
{
a[i] = sc.nextInt();
}
for(i = 0; i < Size; i++)
{
if(a[i] % 2 == 0)
{
EvenSum = EvenSum + a[i];
}
else
{
OddSum = OddSum + a[i];
}
}
System.out.println("\n The Sum of Even Numbers in this Array = " + EvenSum);
System.out.println(" The Sum of Odd Numbers in this Array = " + OddSum);
}
}

任何可被2整除的数字都是偶数。If语句将检查当前数组元素除以2的余数是否恰好等于0。
- 如果条件为True,则为偶数,Java编译器会将该值添加到EvenSum。
- 如果条件为False,则为奇数。编译器会将该值添加到OddSum。
for(i = 0; i < Size; i++) {
if(a[i] % 2 == 0) {
EvenSum = EvenSum + a[i];
}
else {
OddSum = OddSum + a[i];
}
}
用户插入的数组值为a[5] = {10, 15, 20, 55, 95}}
第一次迭代: for (i = 0; 0 < 5; 0++)
i的值将为0,For循环条件(i < 5)为True。
if(a[i] % 2 == 0) => if(a[0] % 2 == 0)
if(10 % 2 == 0) – 条件为True。
EvenSum = EvenSum + a[0]
EvenSum = 0 + 10 = 10
第二次迭代: for (i = 1; 1 < 5; 1++)
条件 (i < 5) 为 True。
if(a[1] % 2 == 0)
if(15 % 2 == 0) – 条件为False。
OddSum = OddSum + a[1]
OddSum = 0 + 15 = 15
第三次迭代: for (i = 2; 2 < 5; 2++)
i的值将为0,条件(i < 5)为True。
if(a[2] % 2 == 0)
if(20 % 2 == 0) – 条件为True。
EvenSum = EvenSum + a[2]
EvenSum = 10 + 20 = 30
对剩余的迭代执行相同的操作,直到条件 (i < 5) 不满足为止。
Java 程序使用 While 循环查找数组中奇数和偶数的总和
此程序计算数组中奇数和偶数的总和与上面相同。但这次,我们使用了Java While循环。
import java.util.Scanner;
public class SumOfEvenOdds2 {
private static Scanner sc;
public static void main(String[] args)
{
int Size, i = 0, j = 0, EvenSum = 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)
{
EvenSum = EvenSum + a[j];
}
else
{
OddSum = OddSum + a[j];
}
j++;
}
System.out.println("\n The Sum of Even Numbers in this Array = " + EvenSum);
System.out.println(" The Sum of Odd Numbers in this Array = " + OddSum);
}
}
Please Enter Number of elements in an array : 8
Please Enter 8 elements of an Array : 15 25 20 40 60 89 97 200
The Sum of Even Numbers in this Array = 320
The Sum of Odd Numbers in this Array = 226
Java 程序使用方法查找数组中奇数和偶数的总和
此程序与第一个示例相同。但这次,我们创建了一个单独的方法来计算偶数总和,并创建了另一个方法来计算奇数总和。
import java.util.Scanner;
public class SumOfEvenOdds3 {
private static Scanner sc;
public static void main(String[] args)
{
int Size, i, EvenSum = 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 : ");
for (i = 0; i < Size; i++)
{
a[i] = sc.nextInt();
}
EvenSum = SumOfEvensinArray(a, Size);
OddSum = SumOfOddsinArray(a, Size);
System.out.println("\n The Sum of Even Numbers in this Array = " + EvenSum);
System.out.println(" The Sum of Odd Numbers in this Array = " + OddSum);
}
public static int SumOfEvensinArray(int[] a, int Size)
{
int i, EvenSum = 0;
for(i = 0; i < Size; i++)
{
if(a[i] % 2 == 0)
{
EvenSum = EvenSum + a[i];
}
}
return EvenSum;
}
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;
}
}
Please Enter Number of elements in an array : 10
Please Enter 10 elements of an Array : 14 24 59 89 100 209 77 16 125 196
The Sum of Even Numbers in this Array = 350
The Sum of Odd Numbers in this Array = 559