对数组中的元素执行算术运算 - android / java

时间:2012-06-16 15:36:39

标签: java android arrays logic

我的应用程序中的计算就是这样..

有一个edittext框,其中来自用户的输入(数字)被解析为int nos

基于输入,出现另外两组edittext框。

如果nos为3,则来自arrayOfEditText的3个edittext和来自arrayOfEditText1的3个edittext出现.. 并且在这些edittext集合中输入的值用于计算。

不完整的java代码在下面..

    calculate.setOnClickListener(new View.OnClickListener() {
                            @Override public void onClick(View v) {
                    // TODO Auto-generated method stub
                    chk();          
}       
});     
}   
public void chk()   {   
            EditText[] arrayOfEditText = new EditText[11];
            arrayOfEditText[1] = ((EditText)findViewById(R.id.EditText01));
            arrayOfEditText[2] = ((EditText)findViewById(R.id.EditText02));
            arrayOfEditText[3] = ((EditText)findViewById(R.id.EditText03));
            arrayOfEditText[4] = ((EditText)findViewById(R.id.EditText04));
            arrayOfEditText[5] = ((EditText)findViewById(R.id.EditText05));
            arrayOfEditText[6] = ((EditText)findViewById(R.id.EditText06));
            arrayOfEditText[7] = ((EditText)findViewById(R.id.EditText07));
            arrayOfEditText[8] = ((EditText)findViewById(R.id.EditText08));
            arrayOfEditText[9] = ((EditText)findViewById(R.id.EditText09));
            arrayOfEditText[10] = ((EditText)findViewById(R.id.EditText10));

            EditText[] arrayOfEditText1 = new EditText[11];
            arrayOfEditText1[1] = ((EditText)findViewById(R.id.EditText11));
            arrayOfEditText1[2] = ((EditText)findViewById(R.id.EditText12));
            arrayOfEditText1[3] = ((EditText)findViewById(R.id.EditText13));
            arrayOfEditText1[4] = ((EditText)findViewById(R.id.EditText14));
            arrayOfEditText1[5] = ((EditText)findViewById(R.id.EditText15));
            arrayOfEditText1[6] = ((EditText)findViewById(R.id.EditText16));
            arrayOfEditText1[7] = ((EditText)findViewById(R.id.EditText17));
            arrayOfEditText1[8] = ((EditText)findViewById(R.id.EditText18));
            arrayOfEditText1[9] = ((EditText)findViewById(R.id.EditText19));
            arrayOfEditText1[10] = ((EditText)findViewById(R.id.EditText20));

            for(int i=1;i<=nos;i++)
            {
                if(arrayOfEditText[i].getText().toString().equals("")||arrayOfEditText1[i].getText().toString().equals(""))
                {
                    Toast.makeText(getApplicationContext(), "Dont leave points empty", 0).show();
                }
                else
                {
                    calcul();
                }
            }   }   public void calcul()    {       EditText[] arrayOfEditText = new EditText[11];
            arrayOfEditText[1] = ((EditText)findViewById(R.id.EditText01));
            arrayOfEditText[2] = ((EditText)findViewById(R.id.EditText02));
            arrayOfEditText[3] = ((EditText)findViewById(R.id.EditText03));
            arrayOfEditText[4] = ((EditText)findViewById(R.id.EditText04));
            arrayOfEditText[5] = ((EditText)findViewById(R.id.EditText05));
            arrayOfEditText[6] = ((EditText)findViewById(R.id.EditText06));
            arrayOfEditText[7] = ((EditText)findViewById(R.id.EditText07));
            arrayOfEditText[8] = ((EditText)findViewById(R.id.EditText08));
            arrayOfEditText[9] = ((EditText)findViewById(R.id.EditText09));
            arrayOfEditText[10] = ((EditText)findViewById(R.id.EditText10));

            EditText[] arrayOfEditText1 = new EditText[11];
            arrayOfEditText1[1] = ((EditText)findViewById(R.id.EditText11));
            arrayOfEditText1[2] = ((EditText)findViewById(R.id.EditText12));
            arrayOfEditText1[3] = ((EditText)findViewById(R.id.EditText13));
            arrayOfEditText1[4] = ((EditText)findViewById(R.id.EditText14));
            arrayOfEditText1[5] = ((EditText)findViewById(R.id.EditText15));
            arrayOfEditText1[6] = ((EditText)findViewById(R.id.EditText16));
            arrayOfEditText1[7] = ((EditText)findViewById(R.id.EditText17));
            arrayOfEditText1[8] = ((EditText)findViewById(R.id.EditText18));
            arrayOfEditText1[9] = ((EditText)findViewById(R.id.EditText19));
            arrayOfEditText1[10] = ((EditText)findViewById(R.id.EditText20));
                for(i=1;i<=nos;i++)
                {
                  //perform calculation
                }   }

现在要在 //执行计算区域中执行的逻辑是..

arrayofedittext [1] * arrayofedittext1 [1] + arrayofedittext [2] * arrayofedittext1 [2] + arrayofedittext [3] * arrayofedittext1 [3] + etc基于 nos / arrayofedittext [1] + arrayofedittext [2] + arrayofedittext [3] + 等基于 nos

有人可以帮我完成这个编码吗? :)

2 个答案:

答案 0 :(得分:1)

创建全局变量

int[] intEditText new int[nos+1];
int[] intEditText1 new int[nos+1];
....
public void chk()   {   
int test=0;
try {

 for(int i=1;i<=nos+nos;i++) {
  if (i==1 && ((EditText)findViewById(R.id.EditText01))=="") {test=1;}
  if (i==2 && ((EditText)findViewById(R.id.EditText02))=="") {test=1;}
   .....etc.
  if (i==11 && ((EditText)findViewById(R.id.EditText11))=="") {test=1;}
  if (test==1) {
    Toast.makeText(getApplicationContext(), "Dont leave points empty", 0).show();
    break;
  } else {
    if (i==1) {intEditText[i]:=Integer.parseInt(((EditText)findViewById(R.id.EditText1));}
    if (i==2) {intEditText[i]:=Integer.parseInt(((EditText)findViewById(R.id.EditText2));}
    .... etc.
    if (i==11) {intEditText1[i-nos]:=Integer.parseInt(((EditText)findViewById(R.id.EditText11));}
    ....etc.

     calcul();
  {
 }


 } catch(NumberFormatException nbre) {
   System.out.println("Could not parse " + nbre);
 } 
}

public void calcul()    {
int myCalc;

  myCalc = intEditText[1]*intEditText[2]..... / (intEditText[1]*intEditText[2].....)
   //perform calculation
}

答案 1 :(得分:1)

执行相同操作的次数越少,您的应用运行速度就越快。因此,制作EditText[] arrayOfEditTextEditText[] arrayOfEditText1类范围的变量,在onCreate()中定义一次,然后在R.id.EditText**之后不必使用findViewById()。

这是一个检查计算中的任何行是否为空的函数,如果没有,那么total将具有你想要的。

public void calculateTotal() {
    String first;
    String second;

    double firstNum;
    double numerator = 0;
    double denominator = 0;

    // This loops counts from nos down to 1. It checks for empty strings and 
    //   sums up the numerator and denominator along the way.
    for(int index = nos; index > 0; index--) {
        first = arrayOfEditText[index].getText().toString();
        second = arrayOfEditText1[index].getText().toString();

        if(first.isEmpty() || second.isEmpty()) {
            Toast.makeText(getApplicationContext(), "Dont leave any points empty (" + (index + 1) + ")", Toast.LENGTH_LONG).show();
            return; // Invalid input, warn user and bail out of function!
        }

        firstNum = Double.parseDouble(first);
        numerator += firstNum * Double.parseDouble(second);
        denominator += firstNum;
    }

    // Do something with total
    double total = numerator / denominator;
}

更好的积分

arrayOfEditTextarrayOfEditText1定义为对整个类可见,如下所示:

public class Example extends Activity {
    EditText[] arrayOfEditText = new EditText[11];
    EditText[] arrayOfEditText1 = new EditText[11];
    ...

        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            ...  
            arrayOfEditText[1] = (EditText) findViewById(R.id.EditText01);
            etc, etc...

您应该知道数组是零索引列表。因此,数组中的第一个条目(arrayOfEditTextarrayOfEditText1)是 null ,如果您尝试引用arrayOfEditText[0]期望EditText您的应用将崩溃。< / p>

最后,此代码假定每个EditText都是有效的Double,如果不限制非数字条目,则添加检查非常简单。

相关问题