【发布时间】:2018-10-02 19:55:48
【问题描述】:
我正在为课堂作业做作业,我试图弄清楚如何获取数组中数字的平均值,同时排除数字 -999(用于表示缺失数据)我已经弄清楚如何得到总和/平均值/最小值/最大值等等,但我不知道如何从搜索范围中排除 -999。您可以在前几种方法中看到我迄今为止尝试解决的一些问题。我什至在谷歌上找不到任何可以开始解释我现在应该做什么的东西。 这些是我正在遵循的说明,以下是我当前的代码,感谢您提供的任何输入。
/**
* 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;
}
}
【问题讨论】:
-
你的问题到底是什么?