Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 计算数组中没有最高值和最低值的平均值_Java_Arrays_Average - Fatal编程技术网

Java 计算数组中没有最高值和最低值的平均值

Java 计算数组中没有最高值和最低值的平均值,java,arrays,average,Java,Arrays,Average,所以我试图计算一个数组的平均值和和,但是在计算平均值/和之前,我必须去掉最高值和最低值 我有代码用随机双精度填充数组,然后找到最高值和最低值 我不确定的是,是否有办法减去最高值和最低值,或者是否必须将数组中的数据复制到一个新数组中,减去最高值和最低值,然后计算平均值/总和 这就是我目前所拥有的。如果这有点明显,请原谅我,但我还是被难住了,还在上java简介课程 这是到目前为止我的代码 double [] contestantOne = new double[8]; for (int index

所以我试图计算一个数组的平均值和和,但是在计算平均值/和之前,我必须去掉最高值和最低值

我有代码用随机双精度填充数组,然后找到最高值和最低值

我不确定的是,是否有办法减去最高值和最低值,或者是否必须将数组中的数据复制到一个新数组中,减去最高值和最低值,然后计算平均值/总和

这就是我目前所拥有的。如果这有点明显,请原谅我,但我还是被难住了,还在上java简介课程

这是到目前为止我的代码

double [] contestantOne = new double[8];

for (int index=0; index < contestantOne.length; index++) {
    contestantOne [index] = (double) (Math.random()*9) + 1;
}

for (int index=0; index < contestantOne.length; index++) {
    System.out.println( contestantOne [index] + "\n");
}

double contestantOneHigh; contestantOneHigh = contestantOne[0];

for (int index=1; index <contestantOne.length; index++) {    
    if (contestantOne[index] > contestantOneHigh)
        contestantOneHigh = contestantOne[index];
}

System.out.print("The highest value in your array is" 
               + " " + contestantOneHigh);
System.out.println();
System.out.println();

double contestantOneLow; contestantOneLow = contestantOne[0];
for (int index=1; index<contestantOne.length; index++) {   

    if (contestantOne [index] < contestantOneLow)
        contestantOneLow = contestantOne[index];
}    

System.out.print("The lowest value in your array is"
               + " " + contestantOneLow);
System.out.println();
System.out.println();
double[]1=新的双精度[8];
for(int index=0;index对于(int index=1;index再次遍历数组,检查每个元素是否等于COMPLETORONE LOW/High,如果它不是加法计数,当您将其除以COMPLETORE ONE.length-2或COMPLETORE ONE.length时。这将给出您的平均值。

再次遍历数组,检查每个元素是否等于COMPLETORONE LOW/High,如果不是当你把这个除以competitorone.length-2或者仅仅除以competitorone.length时,你就可以得到你的平均值了。

我不明白为什么你需要从数组中删除高值和低值来得到平均值?平均值应该是所有值的总和除以总值:

double sum = 0;
for (int index=0; index < contestantOne.length; index++)
{
    //If you really need to remove highest and lowest value
    if (contestantOne[index] != contestantOneLow && contestantOne[index] != contestantOneHigh)
        sum += contestantOne[i];
}
double avg = sum / (double)(contestantOne.length - 2);
double sum=0;
for(int index=0;index
如上所述删除这些值的问题是,如果数组的高和低不是唯一的(数组[10,13,15,3,15,15,15,3,6]15=高,3=低),那么在计算平均值时,平均值将不正确,因为它将忽略15秒和3秒


我建议存储高索引和低索引,并使用它们。

我不明白为什么需要从数组中删除高值和低值以获得平均值?平均值应该是所有值的总和除以值的总数:

double sum = 0;
for (int index=0; index < contestantOne.length; index++)
{
    //If you really need to remove highest and lowest value
    if (contestantOne[index] != contestantOneLow && contestantOne[index] != contestantOneHigh)
        sum += contestantOne[i];
}
double avg = sum / (double)(contestantOne.length - 2);
double sum=0;
for(int index=0;index
如上所述删除这些值的问题是,如果数组的高和低不是唯一的(数组[10,13,15,3,15,15,15,3,6]15=高,3=低),那么在计算平均值时,平均值将不正确,因为它将忽略15秒和3秒


我建议存储高指数和低指数,并使用它们来代替。

像通常一样计算总和,但为最小值和最大值各保留一个变量,最后将其减去:

double min, max, sum;
min = max = sum = list[0];  // May want to add a check to make sure length > 1
for(int i = 1; i < list.length; i++) {
    double thisValue = list[i];
    sum += thisValue;
    min = Math.min(min, thisValue);
    max = Math.max(max, thisValue);
}
sum -= min + max;
double avg = sum / (list.length - 2);
double min、max、sum;
min=max=sum=list[0];//可能需要添加检查以确保长度>1
for(int i=1;i

当然,您可能需要调整精确的方法,以适合您使用的类。

像通常一样计算总和,但为最小值和最大值各保留一个变量,并在最后减去它们:

double min, max, sum;
min = max = sum = list[0];  // May want to add a check to make sure length > 1
for(int i = 1; i < list.length; i++) {
    double thisValue = list[i];
    sum += thisValue;
    min = Math.min(min, thisValue);
    max = Math.max(max, thisValue);
}
sum -= min + max;
double avg = sum / (list.length - 2);
Arrays.sort(a);
find sum of array a
sum -= a[0] + a[a.length - 1]
return sum / (a.length - 2)
double min、max、sum;
min=max=sum=list[0];//可能需要添加检查以确保长度>1
for(int i=1;i

当然,您可能需要调整精确的方法以适合您使用的类。

如果您要计算一个值数组的平均值,但不知道其中是否有零。
Arrays.sort(a);
find sum of array a
sum -= a[0] + a[a.length - 1]
return sum / (a.length - 2)
我用过:

int[]arrayDays=new int[]{周一、周二、周三、周四、周五、周六、周日};
int totalValue=0;
整数除法=0;

对于(int i=0;i,如果要计算一个值数组的平均值,并且不知道其中是否有零。 我用过:

int[]arrayDays=new int[]{周一、周二、周三、周四、周五、周六、周日};
int totalValue=0;
整数除法=0;

对于(int i=0;i为什么不使用树集而不是数组?这样,元素将被排序,并且您将能够以O(1)复杂度移除较高和最低的元素,而不是遍历整个数组

你只需要通过集合来求和,就这样。你的整个程序将在O(logn)+O(n)时间内运行

publicstaticvoidmain(字符串[]args){
TreeSet CompetitorOne=新的TreeSet();
对于(int-index=0;index<8;index++){
添加((双精度)(Math.random()*9)+1);
}
迭代器迭代器=一个迭代器;
while(iterator.hasNext()){
System.out.println(iterator.next());
}
双人比赛