簡體   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