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编程语言来检查奇数元素或偶数元素的平均值哪个更大。
广告