Java 检查奇数元素的平均值是否大于偶数元素的平均值
在Java中,数组是一个对象。它是一种非基本数据类型,用于存储相同数据类型的值。
根据问题要求,我们需要在给定的数组中找到所有偶数和奇数的总和,并进行比较以确定哪个更大。
注意-数组必须是整数数组。
我们来探讨一下这篇文章,看看如何使用Java编程语言来实现。
举例说明
实例1
Suppose the original array is {14, 49, 55,67, 72, 82}
在找到偶数和奇数的平均值之后,结果为−
Average of even numbers are: 56.0
Average of odd numbers are: 57.0
Average of odd elements is greater.
实例2
Suppose the original array is {17, 49, 12, 36, 25, 56}
在找到偶数和奇数的平均值后,结果将为 –
Average of even numbers are: 34.0
Average of odd numbers are: 30.0
Average of even elements is greater.
实例3
Suppose the original array is {7, 29, 32, 86, 22, 51}
更新数组后,其下一个元素为−{22, 33, 44, 55, 11}
Average of even numbers are: 46.0
Average of odd numbers are: 29.0
Average of even elements is greater.
步骤
- 步骤1 - 声明并初始化一个整数数组。
-
步骤2 - 进行一个for循环,并在其中检查偶数和奇数的条件。
-
步骤3 - 同样对计算的偶数和奇数进行相加。
-
步骤4 - 检查偶数的总和是否大于奇数的总和。
-
步骤5 - 打印结果。
语法
要获取数组的长度(即该数组中的元素数量),有一个内置属性即 length。
以下是其语法−
array.length
其中 ‘array’ 指的是数组引用。
多种方法
我们提供了不同的解决方法。
- 通过使用静态初始化数组
-
通过使用用户定义的方法
让我们逐个查看程序及其输出。
方法1:通过使用静态初始化数组
示例
在这种方法中,数组元素将在程序中进行初始化。然后根据算法检查奇数元素或偶数元素的平均值是否更大。
public class Main{
//main method
public static void main(String args[]){
//Declare and initialize the array elements
int arr[]={ 14, 49, 55,67, 72, 82 };
//get the length of the array
int n = arr.length;
int oddSum=0, evenSum=0, evenCount=0, oddCount=0;
//logic implementation for performing sum of odd and even
for(int i=0; i<n; i++){
if(arr[i]%2==0){
evenSum=evenSum+arr[i];
evenCount++;
} else {
oddSum=oddSum+arr[i];
oddCount++;
}
}
//logic implementation for getting average
double avgOdd=oddSum/oddCount;
double avgEven=evenSum/evenCount;
System.out.println("Average of even numbers are: "+avgEven);
System.out.println("Average of odd numbers are: "+avgOdd);
if(avgEven > avgOdd){
//printing the result for even average
System.out.println("\nAverage of even elements is greater.");
} else {
//printing the result for odd average
System.out.println("Average of odd elements is greater.");
}
}
}
输出
Average of even numbers are: 56.0
Average of odd numbers are: 57.0
Average of odd elements is greater.
方法2:使用用户定义方法
示例
在这个方法中,数组元素将在程序中初始化。然后通过传递数组作为参数调用一个用户定义的方法,并在方法内根据算法来检查奇数元素或偶数元素的平均值是否更大。
public class Main{
//main method
public static void main(String args[]){
//Declare and initialize the array elements
int arr[]={44, 44, 55, 66, 72, 80};
//calling user defined method
avg(arr);
}
//declaring user defined method
public static void avg(int []arr){
//get the length of the array
int n = arr.length;
int oddSum=0, evenSum=0, evenCount=0, oddCount=0;
//logic implementation for performing sum of odd and even
for(int i=0; i<n; i++){
if(arr[i]%2==0){
evenSum=evenSum+arr[i];
evenCount++;
} else {
oddSum=oddSum+arr[i];
oddCount++;
}
}
//logic implementation for getting average
double avgOdd=oddSum/oddCount;
double avgEven=evenSum/evenCount;
System.out.println("Average of even numbers are: "+avgEven);
System.out.println("Average of odd numbers are: "+avgOdd);
if(avgEven > avgOdd){
//printing the result for even average
System.out.println("Average of even elements is greater.");
} else {
//printing the result for odd average
System.out.println("Average of odd elements is greater.");
}
}
}
输出
Average of even numbers are: 61.0
Average of odd numbers are: 55.0
Average of even elements is greater.
在本文中,我们通过使用Java编程语言探讨了如何检查奇数元素的平均值是否大于偶数元素的平均值。