当前位置: 代码迷 >> Android >> 计算器应用程序可在一个edittext中用于两个输入,但不适用于两个以上的输入
  详细解决方案

计算器应用程序可在一个edittext中用于两个输入,但不适用于两个以上的输入

热度:33   发布时间:2023-08-04 10:29:58.0

我的代码为两个输入(即单个editText 5+10=15给出了答案,但对于两个以上的输入(即5+10*2=20给出了答案,并且如果我在显示1 +++ 2的某个值后第二次单击运算符,则给出了答案。 如何在值后仅显示一次,以及如何计算editText完整方程式

我的代码如下

public class MainActivity extends AppCompatActivity  implements View.OnClickListener{
Button reset,root,div,mul,del,four,five,six,seven,eight,nine,one,two,three,minus,plus,zero,deci,eql,percent;
int op1,op2;
String first,second;
   String str ="";
    Character op ='q';
    String displayStr="";
    double num,numtemp,temp;

EditText mCalculatorDisplay1;
    DecimalFormat df = new DecimalFormat("@###########");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        mCalculatorBrain = new CalculatorBrain();
        mCalculatorDisplay1 = (EditText) findViewById(R.id.edittext1);
        one = (Button) findViewById(R.id.one);
        two = (Button) findViewById(R.id.two);
        three = (Button) findViewById(R.id.three);
        four = (Button) findViewById(R.id.four);
        five = (Button) findViewById(R.id.five);
        six = (Button) findViewById(R.id.six);
        seven = (Button) findViewById(R.id.seven);
        eight = (Button) findViewById(R.id.eight);
        nine = (Button) findViewById(R.id.nine);
        zero = (Button) findViewById(R.id.zero);
        plus = (Button) findViewById(R.id.plus);
        minus = (Button) findViewById(R.id.minus);
        mul = (Button) findViewById(R.id.multi);
        div = (Button) findViewById(R.id.div);
        del = (Button) findViewById(R.id.delete);
        eql = (Button) findViewById(R.id.equalTo);
        deci = (Button) findViewById(R.id.decimal);
        root = (Button) findViewById(R.id.root);
        reset = (Button) findViewById(R.id.cancel);
        percent = (Button) findViewById(R.id.percent);
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
        seven.setOnClickListener(this);
        eight.setOnClickListener(this);
        nine.setOnClickListener(this);
        zero.setOnClickListener(this);
        plus.setOnClickListener(this);
        minus.setOnClickListener(this);
        mul.setOnClickListener(this);
        div.setOnClickListener(this);
        del.setOnClickListener(this);
        eql.setOnClickListener(this);
        deci.setOnClickListener(this);
        root.setOnClickListener(this);
        reset.setOnClickListener(this);
        percent.setOnClickListener(this);
//  mCalculatorDisplay1.setKeyListener(DigitsKeyListener.getInstance(true, true));
    }
    private void perform() {
        // TODO Auto-generated method stub
        str = "";
        numtemp = num;

    }
    private void insert(int j) {
        // TODO Auto-generated method stub
        str = str + Integer.toString(j);
        num = Double.valueOf(str);
        displayStr += Integer.toString(j);
        mCalculatorDisplay1.setText(displayStr);

    }

    private void calculate() {
        // TODO Auto-generated method stub
         first=String.valueOf(numtemp);
         second=String.valueOf(num);
        if(op == '+'){
            num = numtemp+num;
        }
        else if(op == '-')
            num = numtemp-num;
        else if(op == '/')
            num = numtemp/num;
        else if(op == '*')
            num = numtemp*num;
else if (op=='%')
            num = numtemp*num/100;
        else if (op=='√') {

            num = Math.sqrt(num);
        }
        mCalculatorDisplay1.setText(first + op + second + "\n"+ "=" + num );



    }

    private void insert(String j) {
        //Insert the  values by clicking button
        str = str+String.valueOf(j);
      String  num = String.valueOf(str);
        displayStr += String.valueOf(j);
        mCalculatorDisplay1.setText(displayStr);
    }



        @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.one:
                insert(1);
                break;
            case R.id.two:
                insert(2);
                break;
            case R.id.three:
                insert(3);
                break;
            case R.id.four:
                insert(4);
                break;
            case R.id.five:
                insert(5);
                break;
            case R.id.six:
                insert(6);
                break;
            case R.id.seven:
                insert(7);
                break;
            case R.id.eight:
                insert(8);
                break;
            case R.id.nine:
                insert(9);
                break;
            case R.id.decimal:
               insert(".");
                break;
            case R.id.percent:
                perform();
                op = '%';
                displayStr += "%";
                break;
            case R.id.zero:
                insert(0);
                break;
            case R.id.plus:
//                if (displayStr.contains("+")){
//                    op = '+';
//                    displayStr+="";
//                    perform();
//                    mCalculatorDisplay1.setText(displayStr);
//                }
//                else
                perform();
                op = '+';
                displayStr+= "+";
                mCalculatorDisplay1.setText(displayStr);
                break;
            case R.id.minus:
                perform();
                op = '-';
                displayStr += "-";
                mCalculatorDisplay1.setText(displayStr);
                break;
            case R.id.multi:
                perform();
                op = '*';
                displayStr += "*";
                mCalculatorDisplay1.setText(displayStr);
                break;
            case R.id.div:
                perform();
                op = '/';
                displayStr += "/";
                mCalculatorDisplay1.setText(displayStr);
                break;
            case R.id.cancel:
                str="";
                displayStr = "";
                mCalculatorDisplay1.setText(displayStr);
                break;
            case R.id.root:
                perform();
                op = '√';
                displayStr += "√";
                mCalculatorDisplay1.setText(displayStr);
                break;
            case R.id.delete:
               str="";
                displayStr  ="";
                displayStr=mCalculatorDisplay1.getText().toString();
                if (displayStr.length() >1 ) {
                    displayStr = displayStr.substring(0, displayStr.length() - 1);
                    mCalculatorDisplay1.setText(displayStr);
                }
                else if (displayStr.length() <=1 ) {
                    displayStr  ="";
                    mCalculatorDisplay1.setText( displayStr);
                }
//                    mCalculatorDisplay1.setText(displayStr.substring(0,displayStr.length()-1));

                break;
            case R.id.equalTo:
                calculate();
                break;
        }

        }
}

您应该使用 。 它是Java的表达式评估器。

为了你的目的

  1. 从editText获取字符串

  2. 将字符串传递到以下段

     Calculable calc = new ExpressionBuilder("5+10*2").build() double result=calc.calculate(); 
  相关解决方案