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

huangapple go评论100阅读模式
英文:

Finding MAX even and odd values in an array

问题

我需要找出给定数组中的最大偶数和最大奇数值。
在这种情况下,我需要控制台打印出最大的偶数为8,最大的奇数为11。
这是从另一个问题中修改的,因为提问有冷却时间。

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
		{

		}

		//Write your solution here
		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 8中使用流(Streams)以及Optional:

```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();
}

参见:DoubleStreamOptionalDouble

英文:

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

得分: 0

只是修复了你的代码,我添加了一个变量来存储在计算平均值时应该用作除数的元素数量。在这种情况下,total 存储了数组中所有元素的总和。

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

得分: 0

以下是翻译好的内容:

这样怎么样

        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);
英文:

How about something like this:

    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);

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

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定