我必須創建類與其他人的主類一起實現,出於某種原因,我沒有得到正確的輸出,我不確定這個錯誤來自哪裏。ArrayOutOfBoundsException錯誤?
預期輸出:
中位數= 44.5
均值= 49.300
SD = 30.581
public class StatPackage {
int count;
double [] scores;
StatPackage() {
count = 0;
scores = new double[500];
}
public void insert (double value) {
if (count < 500){
scores[count] = value;
++ count;
}
}
public double Mean() {
double sum = 0;
//For loop for calculating average or mean
for(int i = 0; i < count; i++){
sum += (scores[i]);
}
double average = sum/count;
return average;
}
public double Median() {
int min;
int tmp;
int size;
for (int i = 0; i < count; i ++)
{
min = i;
for (int pos = i + 1; pos < count; pos ++)
if (scores [pos] < scores [min])
min = pos;
tmp = (int)scores [min];
scores [min] = scores [i];
scores [i] = tmp;
}
double median = 0;
if (count % 2 == 0){
median = (scores[scores.length/2-1] + scores[scores.length/2])/2;
}
else {
median = (scores[((scores.length/2))]);
}
return median;
}
public double Variance() {
double variance = 0;
double sum = 0;
//For loop for getting the variance
for(int i = 0; i < count; i++){
sum += scores[i];
variance += scores[i] * scores[i];
}
double varianceFinal = ((variance/count)-(sum*sum)/(count*count));
return (varianceFinal);
}
public double StdDev (double variance) {
double sum = 0;
for(int i = 0; i < count; i++){
sum += scores[i];
variance += scores[i] * scores[i];
}
double varianceFinal = ((variance/count)-(sum*sum)/(count*count));
return Math.sqrt(varianceFinal);
}
但是,您不想向我們顯示錯誤堆棧跟蹤以幫助找到問題?懶惰 – pczeus
你使用什麼數據來獲得預期的結果? –
我不是懶惰的錯誤不能被看到,因爲它是從我把代碼放入其他代碼,我沒有得到輸入的輸入是由另一個程序給我的。 – swaguire