I have an application which calculates 2 simple averages and 1 complex average. The problem is that the average is calculated from 9 values entered in 9 edit texts, and even if the application is working perfect, the edittexts on other size of screen are not arranged anymore. I tried several things to make it work, but nothing so far, so, I decided that it would be much easier to have only 1 edittext, in the case of simple averages, and the button. In the edittext to enter as much as values as you want, and after every value entered to click on a "+" button, and then, when you entered the all values you wanted, to press an "=" button which shows in the same edittext where you entered the values the average of the values you entered. In the case of the complex average, to have 2 edit text and like in the first, to calculate the average from the values entered in the first, making a simple average, and with the value from the second edittext to calculate the complex average. Hope you understood exactly and could help me with this. Thanks a lot !!!

Here is my:

Medii.java

    package com.cngcnasaud.orar;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Medii extends Activity {

public static ArrayList<String> num = new ArrayList<String>();
double sum, res;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.medii);

    final EditText editTextNum = (EditText)findViewById(R.id.editTextNum);
    Button buttona = (Button)findViewById(R.id.button999);
    Button buttonCalc = (Button)findViewById(R.id.button1000);
    final TextView textViewRes = (TextView)findViewById(R.id.textViewRes);

    buttona.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
             String numCatch=editTextNum.getText().toString().trim();
                if(numCatch.length() != 0){
                       num.add(numCatch);
                    editTextNum.setText("");
                }
                textViewRes.setText("The average is ");
        }
    });

    buttonCalc.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            DecimalFormat format = new DecimalFormat("#.##");
            for(int i=0;i<num.size();i++){
                sum = Integer.parseInt(num.get(i)) + sum;
            }
            res = sum/num.size();
            textViewRes.setText("The average is "+format.format(res));
            res = 0;
            sum = 0;
            num.clear();
        }
    });
}}
有帮助吗?

解决方案

I don't know if I got it right, but you could use the method .split("+") to split your input string into an Array of strings, which are you numbers. Then cast each string separately to int, and continue as before. If you want the answer after the "=", concatenate the input string and result string.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top