# 寻找数组中的最大偶数和最大奇数值

go评论81阅读模式

Finding MAX even and odd values in an array

# 问题

import java.util.Scanner;

public class Question03 {

public static int[] array; // 问题中要使用的数组

public static void main(String[] args) {
int number; // 用于楼梯的变量
if (args == null || args.length == 0) {

int[] tempArray = {2, 4, 6, 8, 11}; // 你可以更改这些值进行测试
array = tempArray;
} else {

}

// 在这里编写你的解决方案
int evenMax = array[0];
int oddMax = array[0];
for (int x = 1; x < array.length; x++) {
if (evenMax % 2 == 0) {
if (evenMax < array[x]) {

}
}
if (oddMax < array[x]) {
oddMax = array[x];
}
}

System.out.println("最大的数是：" + evenMax + "。最小的数是：" + oddMax);
}
}

I need to find the MAX even and odd values of a given array.
In this case I need the console to print that the max even is 8 and the max odd is 11
This is revised from another question because of the cool down time for asking questions

import java.util.Scanner;
public class Question03 {

public static int[] array;//The array to be used in the problem
public static void main(String[] args) {
int number;//Used for the stairs
if(args == null || args.length == 0)
{

int[] tempArray ={2,4,6,8,11};//You may change these values to test
array = tempArray;
}
else
{

}

int evenMax = array[0];
int oddMax = array[0];
for (int x =1; x&lt;array.length; x++){
if(evenMax %2 == 0)
{
if (evenMax&lt;array[x])
{

}
}
if(oddMax&lt;array[x]){
oddMax=array[x];
}
}

System.out.println(&quot;The largest number is: &quot; + evenMax + &quot;. The smallest number is: &quot; +oddMax);

</details>

# 答案1
**得分**: 1

```java
import java.util.Arrays;

public static void main(String[] args) {
int[] array = {1,2,3};
Arrays
.stream(array)
.filter(n -> n > 0)
.average()
.getAsDouble();
}

You can use Streams from Java 8 along with Optional:

import java.util.Arrays;

public static void main(String[] args) {
int[] array = {1,2,3};
Arrays
.stream(array)
.filter(n -&gt; n &gt; 0)
.average()
.getAsDouble();
}

vide: DoubleStream and OptionalDouble

# 答案2

int elements = array.length;
double total = 0;
for(int i=0; i&lt;array.length; i++)
{
if(array[i] &lt; 0)
{
array[i] = 0;
}
if(array[i] == 0)
{
elements--;
}
total += array[i];
}
double average = total / elements;

System.out.println("平均值为：" + average);

P.S. 就像任何算法一样，解决这个问题有许多方法。但在这里，我的意图是根据你的思路来帮助你。

Just fixing your code, I added a variable to store the number of elements that should be used as divisor when calculating the average value. In this case total store the sum of all elements in the array.

int elements = array.length;
double total = 0;
for(int i=0; i&lt;array.length; i++)
{
if(array[i] &lt; 0)
{
array[i] = 0;
}
if(array[i] == 0)
{
elements--;
}
total += array[i];
}
double average = total / elements;

System.out.println(&quot;the average was &quot; +average);

P.S. As any algorithm there are many ways to solve this problem. But, here, my intent was to help you according to your line of thought.

# 答案3

double total = 0;
int count = 0;
for(int i=0; i<array.length; i++)
{
if(array[i] > 0)
{
total += array[i];
++count;
}
}
double average = total / count;
System.out.println("平均值为：" + average);

double total = 0;
int count = 0;
for(double value: array)
{
if(value > 0)
{
total += value;
++count;
}
}
double average = total / count;
System.out.println("平均值为：" + average);

double total = 0;
int count = 0;
for(int i=0; i&lt;array.length; i++)
{
if(array[i] &gt; 0)
{
total += array[i];
++count;
}
}
double average = total / count;
System.out.println(&quot;the average was &quot; +average);

Or even:

double total = 0;
int count = 0;
for(double value: array)
{
if(value &gt; 0)
{
total += value;
++count;
}
}
double average = total / count;
System.out.println(&quot;the average was &quot; +average);

• 本文由 发表于 2020年10月6日 10:59:53
• 转载请务必保留本文链接：https://go.coder-hub.com/64218748.html
• arrays
• java

go 63

go 53

go 70

go 59