为什么我在Android Calculator应用程序中输出ZERO作为输出?

时间:2017-07-04 16:39:56

标签: android

我正在android工作室制作一个计算器应用程序,但是当我点击=运算符时,我总是将结果设为零。请帮我解决错误。代码如下:

public class MainActivity extends AppCompatActivity {

Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0,
        btnAdd, btnSub, btnMul, btnDivide, btnEquals, btnPercentage,
        btnClear, btnErase, btnPoint;
EditText etDisplay;

int num = 0, result = 0;
int operator;
public static final int ADD = 0;
public static final int SUBTRACT = 1;
public static final int MULIPLY = 2;
public static final int DIVIDE = 3;
public static final int PERCENTAGE = 4;

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

    etDisplay = (EditText) findViewById(R.id.etDisplay);
    btn0 = (Button) findViewById(R.id.btn0);
    btn1 = (Button) findViewById(R.id.btn1);
    btn2 = (Button) findViewById(R.id.btn2);
    btn3 = (Button) findViewById(R.id.btn3);
    btn4 = (Button) findViewById(R.id.btn4);
    btn5 = (Button) findViewById(R.id.btn5);
    btn6 = (Button) findViewById(R.id.btn6);
    btn7 = (Button) findViewById(R.id.btn7);
    btn8 = (Button) findViewById(R.id.btn8);
    btn9 = (Button) findViewById(R.id.btn9);
    btnAdd = (Button) findViewById(R.id.btnAdd);
    btnSub = (Button) findViewById(R.id.btnSub);
    btnMul = (Button) findViewById(R.id.btnMul);
    btnDivide = (Button) findViewById(R.id.btnDivide);
    btnEquals = (Button) findViewById(R.id.btnEquals);
    btnPercentage = (Button) findViewById(R.id.btnPercentage);
    btnClear = (Button) findViewById(R.id.btnClear);

    btnClear.setOnClickListener(new View.OnClickListener() {

    /****btnClear is used to clear the contents of EditText****/

        @Override
        public void onClick(View v) {
            etDisplay.setText("");
            num = 0;
            result = 0;
        }
    });

    btn0.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            num = num * 10;
            etDisplay.setText("");
            etDisplay.setText(String.valueOf(num));
        }
    });

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            num = (num * 10) + 1;
            etDisplay.setText("");
            etDisplay.setText(String.valueOf(num));
        }
    });
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            num = (num * 10) + 2;
            etDisplay.setText("");
            etDisplay.setText(String.valueOf(num));
        }
    });
    btn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            num = (num * 10) + 3;
            etDisplay.setText("");
            etDisplay.setText(String.valueOf(num));
        }
    });

/ 类似其他按钮 /

    btn4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /*...*/
        }
    });
    btn5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /*...*/
        }
    });
    btn6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /*...*/
        }
    });
    btn7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /*...*/
        }
    });
    btn8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /*...*/
        }
    });
    btn9.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /*...*/
        }
    });

    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            operator = ADD;
            result = num;
            num = 0;
        }
    });

/ 类似于btnSub,btnMul,btnDivide,btnPercentage /

    btnSub.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /*...*/
        }
    });
    btnMul.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /*...*/
        }
    });

    btnDivide.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           /*...*/
        }
    });
    btnPercentage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /*...*/
        }
    });

    btnEquals.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (operator){
                case ADD : result = result + num;
                case SUBTRACT : result = result - num;
                case MULIPLY : result = result * num;
                case DIVIDE : result = result / num;
                case PERCENTAGE : result = result / 100;
            }
            try {
                etDisplay.setText(String.valueOf(result));
            }
            catch (ArithmeticException e){
                etDisplay.setText("");
            }
            finally {
                num = 0;
                result = 0;
            }
        }
    });



}

}

1 个答案:

答案 0 :(得分:0)

您需要在EXISTS中添加break,否则将执行所有案件

switch

似乎您正在尝试使用小数字case ADD : result = result + num; break; case SUBTRACT : result = result - num; break; case MULIPLY : result = result * num; break; case DIVIDE : result = result / num; break; case PERCENTAGE : result = result / 100; 给您的值小于/ 1001会给您integer division,截断其余的值