2012-12-24 73 views
0

我想在計算器中執行多個操作,例如:2+3*3/7。 如何做到這一點。誰能幫我。計算器的單一操作工作。要在android中爲計算器一次計算多個操作

如何在一個editText中計算多個操作。此代碼是在相同的按鈕單擊方法上添加的。

下面是我的代碼:

btnEqualto.setOnClickListener(new OnClickListener() 

    { 
     @Override 

     public void onClick(View v) 

         { 

        String str2 = edtxt.getText().toString(); 

         if (buttonPressed.equals("+")) 

      { 
       Log.e(TAG,"dot operation="+strDot); 


       if (strDot.equals(".")) 

       { 
        Log.e(TAG, "If Codition :"); 


        String str1[] = str2.split("[+]"); 

        Log.e(TAG,"str1 ="+str1); 

        String strVal11 = str1[0]; 

        String strVal22 = str1[1]; 

     Log.e(TAG, "strValues :" + strVal11 + " & " + strVal22); 

        Double xx = Double.parseDouble(strVal11); 

        Double yy = Double.parseDouble(strVal22); 

     Log.e(TAG, "Value of x :" + xx + " AND y : " + yy); 

        Log.e(TAG, "dot operation"); 


        Double result = xx + yy; 

        Log.e(TAG, "Result :" + result); 

        edtxt.setText(result + ""); 


        Log.e(TAG, "VAlue of Result :" + edtxt); 

       } 

       else 

       { 

        Log.e(TAG, "If Codition :"+strVal2); 

        strDot = ""; 

        String str1[] = str2.split("[+]"); 

        strVal1 = str1[0]; 

        strVal2 = str1[1]; 


          Log.e(TAG, "Esle Condition 4 Pllus strValues :" + strVal1 + " & " + strVal2); 


        x = Integer.parseInt(strVal1); 

        y = Integer.parseInt(strVal2); 

      Log.e(TAG, "Value of x :" + x + " AND y : " + y); 

        int result = x + y; 

        Log.e(TAG, "Result :" + result); 

        edtxt.setText(result + ""); 
                Log.e(TAG, "VAlue of Result :" + edtxt); 

       } 

      } 

      } 

     } 

     ); 
+0

什麼錯誤你好嗎?請發佈Eclipse LogCat窗口中的文本(整個紅色部分)。 – user113215

+1

並且不要禁止「未使用」的警告。 – zmbq

回答

2

嘗試exp4j:a simple mathematical expression parser for java..

exp4j需要一個數學術語,在人類可讀的中間符號和 評估通過Dijkstra的 分流先轉換輸入它的結果Yard算法到後綴(RNP)符號並通過簡單的堆棧算法評估後綴項。

試試吧,看看這對你的作品..