繁体   English   中英

Java OOP显示标准偏差

[英]Java OOP to show the standard deviation

每当我单击计算按钮时,标准偏差总是错误的。 这是我的代码:

int test[] = new int[99];
int counter = 0;
double mean, sd = 0, fmean, square, sd2;

//first button labeled as "accept number"
// after put a numbers i need to click the button to add the numbers into array
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)   {                                         
               counter = counter +1;//to increment counter after the user will clicked the button
        test[0] = Integer.parseInt(jTextField2.getText());


    for (int i=0;i<test.length;i++)
    {
        mean += test[i];

    }

    fmean = mean / counter;

    for (int i=0; i<test.length; i++)
    {
        sd += ((test[i] - fmean)*(test[i] - fmean)) / (test.length - 1);
    }


    sd2 = Math.sqrt(sd);
    jTextField1.setText(String.valueOf(counter));
    jTextField2.setText("");//to clear the text inside the textfield

}


// second button labeled as "compute"
//to show the result when the compute button clicked
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) 
{          



jTextField3.setText(String.valueOf(fmean));


jTextField4.setText(String.valueOf(sd2));
}

我看到两个问题:

  1. 您始终将输入放置在相同的数组位置(即0),这应该是test[counter - 1] = Integer.parseInt(jTextField2.getText()); 代替。
  2. 您的for循环不应运行到test.length (即99),而应运行在counter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM