如何计算类间隔算术平均值?

时间:2013-09-22 02:35:48

标签: java android

我正在编写一个计算类间隔算术平均值的程序,但是它的错误有一点误差......

例如: 我将在我的interval_Values中输入(10-20,21-30),我将在我的frequency_values中输入(1,2),计算就是这样:

((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));

1 个答案:

答案 0 :(得分: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));

        }
        });