0

我正在制作一个计算类区间算术平均值的程序,但它的错误中有一点点错误......

例如:我将在我的 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));
4

1 回答 1

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

        }
        });
于 2013-09-22T09:25:52.310 回答