2013-09-22 142 views
0

我正在做的是計算類區間算術平均值但在它的錯誤有點異常的程序...如何計算類間隔算術平均值?

例如: 我會輸入(10-20,21-30)在我interval_Values我會輸入(1,2)在我frequency_values和雲計算是這樣的:

((10+20)/2),((21+30)/2) 

,它會給

15,25.5 

然後再次將這個值乘到frequency_Values的價值

(15*1)+(25.5*2) 

,這會給

(15+51)=66 

後這些結果。它將要分66到frequency_Values的總和是

(1+3) 

所以

66/3=22 

在我的程序,當我輸入這些值,它給出了15的結果將是什麼錯誤。

final AutoCompleteTextView interval_Values = (AutoCompleteTextView) findViewById(R.id.interval_Values); 
    final AutoCompleteTextView frequency_Values = (AutoCompleteTextView) findViewById(R.id.frequency_Values); 
    final TextView txtSummation = (TextView) findViewById(R.id.txtSummation); 
    final TextView txtArithmetic = (TextView) findViewById(R.id.txtArithmetic); 

    Button btncalculate = (Button) findViewById(R.id.btncalculate); 
    btncalculate.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      String[] interval = interval_Values.getText().toString().split(","); 
      String[] frequency= frequency_Values.getText().toString().split(","); 
      double [] x = new double[interval.length]; 
      double [] y = new double[frequency.length]; 

      double freq=0; 
      double xy=0; 
      double result=0; 

      for(int j=0;j<interval.length;j++){ 
       String[] intr=interval[j].split("-"); 
       x[j]=Double.parseDouble(intr[j]); 

       double midpoint=((x[0])+(x[1]))/2; 

       y[j]=Double.parseDouble(frequency[j]); 
       freq+=y[j]; 
       xy+=midpoint*y[j]; 
       result =xy/freq; 

      } 
      txtArithmetic.setText(Double.toString(result)); 

回答

0

思考很多次後,我想出了這個代碼,回答上述問題...我希望這將有助於:)

final AutoCompleteTextView interval_Values = (AutoCompleteTextView) findViewById(R.id.interval_Values); 
    final AutoCompleteTextView frequency_Values = (AutoCompleteTextView) findViewById(R.id.frequency_Values); 
    final TextView txtSummation = (TextView) findViewById(R.id.txtSummation); 
    final TextView txtArithmetic = (TextView) findViewById(R.id.txtArithmetic); 

    Button btncalculate = (Button) findViewById(R.id.btncalculate); 
    btncalculate.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      String[] interval = interval_Values.getText().toString().split(","); 
      String[] frequency= frequency_Values.getText().toString().split(","); 
      double [] freq=new double[frequency.length]; 




      double xy=0; 
      double y=0; 
      double midpoint=0; 
      for(int j=0;j<interval.length;j++){ 
       String intr[]=interval[j].split("-"); 
        midpoint=(Double.parseDouble(intr[0]) + Double.parseDouble(intr[1]))/2; 
       freq[j]=Double.parseDouble(frequency[j]); 
       y+=freq[j]; 
       xy+=midpoint*freq[j]; 

      } 

      txtSummation.setText(xy+"/"+y); 
      double result=xy/y; 
      txtArithmetic.setText(Double.toString(result)); 

     } 
     });