我正在为班级分配作业,我试图弄清楚如何获取数组中数字的平均值,同时排除数字-999(用于表示缺少的数据),我想出了如何获取总和/平均/最低/最高等,但我无法弄清楚如何从搜索范围中排除-999。您可以在前几种方法中看到到目前为止我已经尝试解决的一些问题。我什至在Google上都找不到任何东西,甚至可能无法开始解释我现在应该做什么。
java - 如何从数组和/平均值/最小值/最大值中排除数字?-LMLPHP
这些是我要遵循的说明,以下是我当前的代码,感谢您的输入。

    /**
 * WeatherComputation.java
 */

//Put any imports below this line.
import java.util.*;

/**
 * Static methods library which compute averages and other
 * computations on integer arrays of temperatures.
 *
 * @author Joel Swanson
 * @version 03.29.2014
 */
public class WeatherComputation
{
    /**
     * Average an array of temperatures.
     * @param temperatures An array storing temperatures as ints.
     * @return Returns the average of the array of ints.
     */
    public static double averageTemperature(int[] temperatures)
    {
        int sum = 0;
        for (int i = 0; i < temperatures.length; i++)
        {
         sum += temperatures[i];
        }
        double average = sum / temperatures.length;
        return average;
    }

    /**
     * Find the highest in an array of temperatures.
     * @param temperatures An array storing temperatures as ints.
     * @return The largest value from the the array of ints.
     */
    public static int highestTemperature(int[] temperatures)
    {
        int max = temperatures[0];
        for (int i = 1; i < temperatures.length; i++)
        {
            if(temperatures[i] > max)
            {
                max = temperatures[i];
            }
        }
        return max;
    }

    /**
     * Find the lowest in an array of temperatures.
     * @param temperatures An array storing temperatures as ints.
     * @return The lowest value from the the array of ints.
     */
    public static int lowestTemperature(int[] temperatures)
    {
        int min = 0;
        Arrays.sort(temperatures);

        while (true)
        {
            if (min == -999)
            {
                break;
            }

            if(min > -999)
            {
                min = temperatures[0];
            }
        }

        for (int i = 1; i < temperatures.length; i++)
        {
            if (min < -999)
            {
                if (temperatures[i] < min)
                {
                    min = temperatures[i];
                }
            }
        }

        return min;
    }

    /**
     * Return the total number of missing days.  That is days with
     * -999 recorded as the temperatures.
     * @param temperatures An array storing temperatures as ints.
     * @return The number of -999s found.
     */
    public static int numberMissing(int[] temperatures)
    {
        int count = 0;

        return count;
    }

    /**
     * Calculate heating degree day.
     * @param max The highest temperature for a given day.
     * @param min The lowest temperature for a given day.
     * @return heating degree day data for this day.
     */
    public static double hdd(int max, int min)
    {
        double hdd = 0.0;

        return hdd;
    }

    /**
     * Calculate cooling degree day.
     * @param max The highest temperature for a given day.
     * @param min The lowest temperature for a given day.
     * @return cooling degree day data for this day.
     */
    public static double cdd(int max, int min)
    {
        double cdd = 0.0;

        return cdd;
    }

    /**
     * Sum monthly heating degree days.
     * @param max An array with the highest temperatures for each day
     * in a given month.
     * @param min An array with the lowest temperatures for each day
     * in a given month.
     * @return The sum of the heating degree days.
     */
    public static double monthHdd(int[] max, int[] min)
    {
        double sum = 0.0;

        return sum;
    }

    /**
     * Sum monthly cooling degree days.
     * @param max An array with the highest temperatures for each day
     * in a given month.
     * @param min An array with the lowest temperatures for each day
     * in a given month.
     * @return The sum of the cooling degree days.
     */
    public static double monthCdd(int[] max, int[] min)
    {
        double sum = 0.0;

        return sum;
    }
}

最佳答案

对于最低温度,这就是我要做的:

public static int lowestTemperature(int[] temperatures) {
    int min = 999;
    for (int temperature : temperatures) {
        if (temperature > -999 && temperature < min) {
            min = temperature;
        }
    }
    return min;
}


假设您的WeatherComputation类还将考虑999的方式与考虑-999的方式相同(无效值)。原因很简单,如果一个月内只有负值怎么办?然后,您的int min = 0将不正确。

至于平均温度:

public static double averageTemperature(int[] temperatures) {
    int sum = 0, validTemperatureCounter = 0;
    for (int temperature : temperatures) {
        if (temperature > -999 && temperature < 999) {
            validTemperatureCounter++;
            sum += temperature;
        }
    }
    return sum / (double)validTemperatureCounter;
}


我也无视值999的自由。

更新资料

为了好玩,有一个班轮:

public static double averageTemperatureOneLiner(int[] temperatures) {
    return Arrays.stream(temperatures).filter(t -> t > -999 && t < 999).average().orElse(0);
}


public static int lowestTemperatureOneLiner(int[] temperatures) {
    return Arrays.stream(temperatures).filter(t -> t > -999 && t < 999).min().orElse(0);
}


public static int highestTemperatureOneLiner(int[] temperatures) {
    return Arrays.stream(temperatures).filter(t -> t > -999 && t < 999).max().orElse(0);
}

关于java - 如何从数组和/平均值/最小值/最大值中排除数字?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49969753/

10-11 22:37
查看更多