确定Java中的用户提供的阵列的最大值和平均值 [英] Determining Maximum and Mean of an User-Provided Array in Java
问题描述
我是新来的编码所以很容易在我身上。我试图确定最大和使用两个单独的类(即某某单独xyztester类)的用户提供数组的意思。我已经有我的编码,但最大的输出是0.0,且平均输出比阵列的长度少一个。这里是我的编码 -
I am new to coding so be easy on me. I am trying to determine the maximum and mean of an user-provided array using two separate classes (i.e. xyz and a separate xyztester class). I've have my coding but the maximum output is 0.0 and the mean output is one less than the length of the array. Here is my coding -
XYZ类
public static double maximum(double[] array){
double max = array[0];
for (int j = 1; j < array.length; j++){
if(array[j] > max){
max = array[j];
}
}
return max;
}
public static double mean(double[] array){
double sum = 0;
for (double k = 0; k < array.length; k++)
sum = sum + array.length;
double avg = sum / array.length;
return avg;
}
xyzTester级
"xyzTester" class
double [] b;
b = new double [quantArray];
int j;
for (j = 0; j > quantArray; j++){
b[j] = in.nextDouble();
}
double n = xyz.maximum(b);
double [] c;
c = new double [quantArray];
int k;
for (k = 0; k > quantArray; k++){
c[k] = in.nextDouble();
}
double o = xyz.mean(c);
有人可以详细说一下我做错了?
Can someone detail what I am doing wrong?
推荐答案
我看到了两个问题:在平均法
I see two problems:In the mean method
sum = sum + array.length;
也许应该是
sum = sum + array[k];
其次所有浮点计算应该是浮点operants之间。因此,更好地施放的东西像数组的长度来划分之前增加一倍:
Secondly all floating point calculations should be between floating point operants. So better cast stuff like array length to double before dividing:
double avg = sum / (double)array.length;
这篇关于确定Java中的用户提供的阵列的最大值和平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!