计算器代码减去func不起作用

时间:2017-10-19 13:01:05

标签: java android avd

我需要有关算法的帮助我不明白我做错了什么,当我做减去func它给出一个减号(不是正确的),当我做加号然后减去数字真的真的错了,我如何在每个功能之后设置停止并且喜欢让它正确计算我已经尝试了很长时间才这样做我只是不知道在编码时使用什么算法

button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
buttonClear = (Button) findViewById(R.id.buttonClear);
buttonPlus = (Button) findViewById(R.id.buttonPlus);
buttonEquals = (Button) findViewById(R.id.buttonEquals);
buttonMinus = (Button) findViewById(R.id.buttonMinus);
textFinal = (TextView) findViewById (R.id.textFinal);
int resultValue=0;


buttonPlus.setOnClickListener(
    new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            String s = (String) textFinal.getText();
            ResultValue = ResultValue + Integer.parseInt(s);
            textFinal.setText("");
        }
    });
buttonMinus.setOnClickListener (
    new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            String s = (String) textFinal.getText();
            ResultValue = ResultValue - Integer.parseInt(s);
            textFinal.setText("");

        }
    });

buttonEquals.setOnClickListener(
    new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            String s = (String) textFinal.getText();
            ResultValue = Integer.parseInt(s)+ResultValue ;
            textFinal.setText(Integer.toString(ResultValue));
            ResultValue=0;
        }
    });

我尝试输入一个名为LastAction的新int并在一次操作后停止并更新计算。 我尝试了更多我无法记住的事情。

我试过了:

int LastAction = 0 , and if (LastAction = 1) {
String s = (String) textFinal.getText();
                    ResultValue = ResultValue - Integer.parseInt(s);
                    textFinal.setText("");}
LastAction=1; 

1 个答案:

答案 0 :(得分:0)

    private int operationType;   

     buttonMinus.setOnClickListener (
            new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    String s = (String) textFinal.getText();
                    ResultValue =  Integer.parseInt(s);
                    operationType = 1;
                    textFinal.setText("");

                }
            });

buttonEquals.setOnClickListener(
    new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            String s = (String) textFinal.getText();
            switch(operationType)
            {
                  case 1:// it means minus
                        ResultValue = ResultValue - Integer.parseInt(s); 
                        break;
                  case 2:// it means add
                        ResultValue = ResultValue + Integer.parseInt(s);
                        break;
            }
            textFinal.setText(Integer.toString(ResultValue));
            ResultValue=0;
        }
    });