[英]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));
}
我看到兩個問題:
test[counter - 1] = Integer.parseInt(jTextField2.getText());
代替。 for
循環不應運行到test.length
(即99),而應運行在counter
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.