计算器应用程序没有响应相等的按钮

时间:2018-01-28 22:14:18

标签: android calculator

在我的简单计算器项目中,当我按下相等按钮时程序退出,它表示我的程序没有响应。在android监视器中我找不到任何错误。相等按钮应该看到用户输入了什么数学运算符,假设它已被输入一次,之后它获得该运算符之前和之后的数值并执行所需的数学运算功能。这是我的代码:

[3 9 8 9 6 6 8 3 8 9]
[0.48244034708477557, 0.46697514806874096, 0.46860265801660717]
[4.792020458960906, 4.818586917826906, 4.785608761012554]

如果有任何遗漏信息,请告诉我添加它。提前致谢 编辑: 这是错误:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    public boolean isEmpty(EditText editText) {
        if (editText.getText().toString().trim().length() > 0)
            return false;

        return true;
    }
Button number9;Button number8;Button number7;Button number6;Button number5;Button number4;Button number3;Button number2;
    Button number1;Button number0;Button numberplus;Button numberminus;Button numbertimes;Button numberdiv;Button numberequal;
    Button numberdecimal;EditText text_;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        number0= (Button) findViewById(R.id.btn0);
        number1= (Button) findViewById(R.id.btn1);
        number2= (Button) findViewById(R.id.btn2);
        number3= (Button) findViewById(R.id.btn3);
        number4= (Button) findViewById(R.id.btn4);
        number5= (Button) findViewById(R.id.btn5);
        number6= (Button) findViewById(R.id.btn6);
        number7= (Button) findViewById(R.id.btn7);
        number8= (Button) findViewById(R.id.btn8);
        number9= (Button) findViewById(R.id.btn9);
        numberplus= (Button) findViewById(R.id.btnplus);
        numbertimes= (Button) findViewById(R.id.btntimes);
        numberminus= (Button) findViewById(R.id.btnminus);
        numberequal= (Button) findViewById(R.id.btnequal);
        numberdiv= (Button) findViewById(R.id.btndiv);
        numberdecimal=  (Button) findViewById(R.id.btndecimal);
        text_=(EditText) findViewById(R.id.editText);
        number0.setOnClickListener(this);
        number1.setOnClickListener(this);
        number2.setOnClickListener(this);
        number3.setOnClickListener(this);
        number4.setOnClickListener(this);
        number5.setOnClickListener(this);
        number6.setOnClickListener(this);
        number7.setOnClickListener(this);
        number8.setOnClickListener(this);
        number9.setOnClickListener(this);
        numberplus.setOnClickListener(this);
        numberminus.setOnClickListener(this);
        numbertimes.setOnClickListener(this);
        numberdiv.setOnClickListener(this);
        numberequal.setOnClickListener(this);
        numberdecimal.setOnClickListener(this);
        text_.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){

            case R.id.btn0:
                text_.append("0");
                break;

            case R.id.btn1:
                text_.append("1");
                break;

            case R.id.btn2:
                text_.append("2");
                break;

            case R.id.btn3:
                text_.append("3");
                break;

            case R.id.btn4:
                text_.append("4");
                break;

            case R.id.btn5:
                text_.append("5");
                break;

            case R.id.btn6:
                text_.append("6");
                break;

            case R.id.btn7:
                text_.append("7");
                break;

            case R.id.btn8:
                text_.append("8");
                break;

            case R.id.btn9:
                text_.append("9");
                break;



            case R.id.btnplus:
                text_.append("+");
                break;

            case R.id.btnminus:
                text_.append("-");
                break;

            case R.id.btndiv:
                text_.append("/");
                break;

            case R.id.btntimes:
                text_.append("*");
                break;

            case R.id.btndecimal:
                text_.append(".");
                break;

            case R.id.btnequal:
                String textString = text_.getText().toString();
                if(textString.contains("+")) {
                    String before = textString.substring(textString.indexOf("+") - 1);
                    String after=textString.substring(textString.indexOf("+") + 1);
                    float number_after=Float.parseFloat(after);
                    float number_before=Float.parseFloat(before);
                    float result = number_before + number_after;
                    String final_result =String.valueOf(result);
                    text_.setText(final_result);

                }
                if(textString.contains("-")) {
                    String before = textString.substring(textString.indexOf("-") - 1);
                    String after=textString.substring(textString.indexOf("-") + 1);
                    float number_after=Float.parseFloat(after);
                    float number_before=Float.parseFloat(before);
                    float result = number_before - number_after;
                    String final_result =String.valueOf(result);
                    text_.setText(final_result);

                }
                if(textString.contains("*")) {
                    String before = textString.substring(textString.indexOf("*") - 1);
                    String after=textString.substring(textString.indexOf("*") + 1);
                    float number_after=Float.parseFloat(after);
                    float number_before=Float.parseFloat(before);
                    float result = number_before * number_after;
                    String final_result =String.valueOf(result);
                    text_.setText(final_result);

                }
                if(textString.contains("/")) {
                    String before = textString.substring(textString.indexOf("/") - 1);
                    String after=textString.substring(textString.indexOf("/") + 1);
                    float number_after=Float.parseFloat(after);
                    float number_before=Float.parseFloat(before);
                    float result = number_before / number_after;
                    String final_result =String.valueOf(result);
                    text_.setText(final_result);

                }

                break;
        }

    }
}

2 个答案:

答案 0 :(得分:0)

你做了太多复制粘贴查看你的条件,如果你检查 - ,*或/你仍然将你的字符串拆分为'+'而不是 - 或*或/你需要改变它,这样你得到索引边界异常

答案 1 :(得分:0)

here is the issue      

    String before = textString.substring(textString.indexOf("+") - 1);
    String after=textString.substring(textString.indexOf("+") + 1);


if input is "52+34"
before = 2+34
after = 34

Solution


String before = textString.substring(0,textString.indexOf("+"));
String after=textString.substring(textString.indexOf("+"), textString.length());

before = 52
after = 34

rest of the code to get the result looks OK.