[英]Java OOP to show the standard deviation
Whenever I click the compute button the standard deviation is always wrong. 每当我单击计算按钮时,标准偏差总是错误的。 Here's my code: 这是我的代码:
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));
}
I see two problems: 我看到两个问题:
test[counter - 1] = Integer.parseInt(jTextField2.getText());
您始终将输入放置在相同的数组位置(即0),这应该是test[counter - 1] = Integer.parseInt(jTextField2.getText());
instead. 代替。 for
loops should not run to test.length
(ie 99), but to counter
. 您的for
循环不应运行到test.length
(即99),而应运行在counter
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.