为什么我的代码出来的错误的输出?是我的insert类错了吗? [英] Why is my code coming out with the wrong output? Is my insert class wrong?

查看:98
本文介绍了为什么我的代码出来的错误的输出?是我的insert类错了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建类,以实现与主类别的人有一些原因,由于某种原因,我没有得到正确的输出,我不知道是我的计算,我不认为他们是或我insert type is wrong。

I have to create classes to be implemented with the main class someone else has and for some reason I am not getting the right outputs, I'm not sure is my calculations are off which I don't think they are or my insert class is wrong.

预期输出:

Median = 44.5

Median = 44.5

平均= 49.300

Mean = 49.300

SD = 30.581

SD = 30.581

实际输出:

中位数= 0.0

平均= 0.967

SD = 4.712

SD = 4.712

public class StatPackage { 
int count; 
double [] scores; 
final int MAX = 500; 


StatPackage() { 
count = 0; 
scores = new double[MAX]; 
} 
public void insert (double value) { 
if (count < MAX){ 
scores[count] = value; 
++ count; 
} 
} 
public double Mean () { 
    double sum = 0; 
    //For loop for calculating average or mean
    for(int i = 0; i < scores.length; i++){
            sum += (scores[i]);
            count++;    
    }
    double average = sum/count;  
    return average;
    } 

public double Median() { 
int min; 
int tmp; 
int size; 

for (int i = 0; i < scores.length - 1; i ++) 
{ 
min = i; 
for (int pos = i + 1; pos < scores.length; pos ++) 
if (scores [pos] < scores [min]) 
min = pos; 

tmp = (int)scores [min]; 
scores [min] = scores [i]; 
scores [i] = tmp; 

} 
double median = 0;
if  (scores.length % 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 < scores.length; i++){
        sum += scores[i];
        variance += scores[i] * scores[i];
        count++;
    }
    double varianceFinal = ((variance/count)-(sum*sum)/(count*count));
    return (varianceFinal);
} 

public double StdDev (double variance) { 
    double sum = 0;
    for(int i = 0; i < scores.length; i++){
        sum += scores[i];
        variance += scores[i] * scores[i];
        count++;
    }
    double varianceFinal = ((variance/count)-(sum*sum)/(count*count));
return Math.sqrt(varianceFinal);

}

}   


推荐答案

因为解决方案很容易通过调试找到,我只会给你一个提示:

Because the solution is easily found by debugging, I will only give you a hint:


$ c> mean of 3,4 and 5 is 4:(3 + 4 + 5)/ 3 ,not (3 + 4 + 5)/(n * 3)其中
n 是正整数。 $ b

The mean of 3, 4 and 5 is 4: (3+4+5)/3, not (3+4+5)/(n*3) where n is a positive integer.

如果你看看你的意思和std并除以预期结果,你会看到它是一个四舍五入的数字。

If you look at your mean and std and divide it by the expected result, you will see it's a rounded number.

一旦找到1个问题的解决方案,您就会马上知道为什么其他结果也是错误的)=)

Once you find the solution to 1 problem, you will immediately know why the other results are faulty as well =)

这篇关于为什么我的代码出来的错误的输出?是我的insert类错了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆