如何在计算器的结果中添加小数?

时间:2014-09-12 18:53:52

标签: java android

我正在为Android Wear制作计算器,一切顺利,但我遇到了问题 十进制数。当我做一个像95/2这样的简单操作时,结果是47而不是47.5。 此外,当我写一个十进制数字,如5.5或9.4,然后我点击+, - ,*或/,模拟器说“遗憾的是应用程序停止了”。 这是我的活动:

public class MyActivity extends Activity implements View.OnClickListener {

    Button button0, button1, button2, button3, button4, button5, button6 , button7, button8, button9,
            buttonClear, buttonDelete, buttonEqual, buttonMin, buttonPlus, buttonX,
            buttonSlash, buttonDot;
    EditText editText;
    int op1;
    int op2;
    String optr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rect_activity_my);
        final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);

        editText = (EditText) findViewById(R.id.editText);

        button0 = (Button) findViewById(R.id.button0);
        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);
        buttonEqual = (Button) findViewById(R.id.buttonEqual);
        buttonSlash = (Button) findViewById(R.id.buttonSlash);
        buttonDelete = (Button) findViewById(R.id.buttonDelete);
        buttonX = (Button) findViewById(R.id.buttonX);
        buttonMin = (Button) findViewById(R.id.buttonMin);
        buttonPlus = (Button) findViewById(R.id.buttonPlus);
        buttonDot = (Button) findViewById(R.id.buttonDot);

        try{
            button0.setOnClickListener(this);
            button1.setOnClickListener(this);
            button2.setOnClickListener(this);
            button3.setOnClickListener(this);
            button4.setOnClickListener(this);
            button5.setOnClickListener(this);
            button6.setOnClickListener(this);
            button7.setOnClickListener(this);
            button8.setOnClickListener(this);
            button9.setOnClickListener(this);
            buttonClear.setOnClickListener(this);
            buttonDelete.setOnClickListener(this);
            buttonEqual.setOnClickListener(this);
            buttonMin.setOnClickListener(this);
            buttonPlus.setOnClickListener(this);
            buttonX.setOnClickListener(this);
            buttonSlash.setOnClickListener(this);
            buttonDot.setOnClickListener(this);
        }

        catch(Exception e){

        }
    }

    public void operation(){
        if(optr.equals("+")){
            op2 = Integer.parseInt(editText.getText().toString());
            editText.setText("");
            op1 = op1 + op2;
            editText.setText("Result : " + Integer.toString(op1));
        }
        else if(optr.equals("-")){
            op2 = Integer.parseInt(editText.getText().toString());
            editText.setText("");
            op1 = op1 - op2;
            editText.setText("Result : " + Integer.toString(op1));
        }
        else if(optr.equals("*")){
            op2 = Integer.parseInt(editText.getText().toString());
            editText.setText("");
            op1 = op1 * op2;
            editText.setText("Result : " + Integer.toString(op1));
        }
        else if(optr.equals("/")){
            op2 = Integer.parseInt(editText.getText().toString());
            editText.setText("");
            op1 = op1 / op2;
            editText.setText("Result : " + Integer.toString(op1));
        }
    }

    @Override
    public void onClick(View arg0) {
        Editable str =  editText.getText();
        switch(arg0.getId()){
            case R.id.button1:
                if(op2 != 0){
                    op2 = 0;
                    editText.setText("");
                }
                str = str.append(button1.getText());
                editText.setText(str);
                break;
            case R.id.button2:
                if(op2 != 0){
                    op2 = 0;
                    editText.setText("");
                }
                str = str.append(button2.getText());
                editText.setText(str);
                break;
            case R.id.button3:
                if(op2 != 0){
                    op2 = 0;
                    editText.setText("");
                }
                str = str.append(button3.getText());
                editText.setText(str);
                break;
            case R.id.button4:
                if(op2 != 0){
                    op2 = 0;
                    editText.setText("");
                }
                str = str.append(button4.getText());
                editText.setText(str);
                break;
            case R.id.button5:
                if(op2 != 0){
                    op2 = 0;
                    editText.setText("");
                }
                str = str.append(button5.getText());
                editText.setText(str);
                break;
            case R.id.button6:
                if(op2 != 0){
                    op2 = 0;
                    editText.setText("");
                }
                str = str.append(button6.getText());
                editText.setText(str);
                break;
            case R.id.button7:
                if(op2 != 0){
                    op2 = 0;
                    editText.setText("");
                }
                str = str.append(button7.getText());
                editText.setText(str);
                break;
            case R.id.button8:
                if(op2 != 0){
                    op2 = 0;
                    editText.setText("");
                }
                str = str.append(button8.getText());
                editText.setText(str);

                break;
            case R.id.button9:
                if(op2 != 0){
                    op2 = 0;
                    editText.setText("");
                }
                str = str.append(button9.getText());
                editText.setText(str);

                break;
            case R.id.button0:
                if(op2 != 0){
                    op2 = 0;
                    editText.setText("");
                }
                str = str.append(button0.getText());
                editText.setText(str);

                break;
            case R.id.buttonClear:
                op1 = 0;
                op2 = 0;
                editText.setText("");

                break;
            case R.id.buttonDot:                                                     
                if (op1 == 0){
                    op1 = Integer.parseInt(editText.getText().toString());
                    editText.setText(op1 + ".");
                } else if (op2 == 0){
                    op2 = Integer.parseInt(editText.getText().toString());
                    editText.setText(op2 + ".");
                }

                break;
            case R.id.buttonDelete:
                editText.setText(str);
                if (str.length() > 1){
                    str = (Editable) str.subSequence(0, str.length() - 1);
                    editText.setText(str);
                }else if (str.length() <= 1){
                    editText.setText("");
                }

                break;
            case R.id.buttonPlus:
                optr = "+";
                if(op1 == 0){
                    op1 = Integer.parseInt(editText.getText().toString());
                    editText.setText("");
                }
                else if(op2 != 0){
                    op2 = 0;
                    editText.setText("");
                }
                else{
                    op2 = Integer.parseInt(editText.getText().toString());
                    editText.setText("");
                    op1 = op1 + op2;
                    editText.setText("Result : " + Integer.toString(op1));
                }

                break;
            case R.id.buttonMin:
                optr = "-";
                if(op1 == 0){
                    op1 = Integer.parseInt(editText.getText().toString());
                    editText.setText("");
                }
                else if(op2 != 0){
                    op2 = 0;
                    editText.setText("");
                }
                else{
                    op2 = Integer.parseInt(editText.getText().toString());
                    editText.setText("");
                    op1 = op1 - op2;
                    editText.setText("Result : " + Integer.toString(op1));
                }

                break;
            case R.id.buttonX:
                optr = "*";
                if(op1 == 0){
                    op1 = Integer.parseInt(editText.getText().toString());
                    editText.setText("");
                }
                else if(op2 != 0){
                    op2 = 0;
                    editText.setText("");
                }
                else{
                    op2 = Integer.parseInt(editText.getText().toString());
                    editText.setText("");
                    op1 = op1 * op2;
                    editText.setText("Result : " + Integer.toString(op1));
                }

                break;
            case R.id.buttonSlash:
                optr = "/";
                if(op1 == 0){
                    op1 = Integer.parseInt(editText.getText().toString());
                    editText.setText("");
                }
                else if(op2 != 0){
                    op2 = 0;
                    editText.setText("");
                }
                else{
                    op2 = Integer.parseInt(editText.getText().toString());
                    editText.setText("");
                    op1 = op1 / op2;
                    editText.setText("Result : " + Integer.toString(op1));
                }

                break;
            case R.id.buttonEqual:
                if(!optr.equals(null)){
                    if(op2 != 0){
                        if(optr.equals("+")){
                            editText.setText("");
                            /*op1 = op1 + op2;*/
                            editText.setText("Result : " + Integer.toString(op1));
                        }
                        else if(optr.equals("-")){
                            editText.setText("");/*
                            op1 = op1 - op2;*/
                            editText.setText("Result : " + Integer.toString(op1));
                        }
                        else if(optr.equals("*")){
                            editText.setText("");/*
                            op1 = op1 * op2;*/
                            editText.setText("Result : " + Integer.toString(op1));
                        }
                        else if(optr.equals("/")){
                            editText.setText("");/*
                            op1 = op1 / op2;*/
                            editText.setText("Result : " + Integer.toString(op1));
                        }
                    }
                    else{
                        operation();
                    }
                }
                break;
        }
    }
}

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

Java中,整数除法会自动截断任何小数值。

解决此问题的一种方法是将除法定义op1 = op1 / op2;修改为类似于以下内容:

op1 = op1 / (double) op2

语句Integer.parseInt()在转换十进制数时会出现问题,因为整数不能包含小数。这可能就是为什么你有错误&#34;不幸的是应用程序已停止&#34;当你尝试输入带小数的数字时。

要考虑小数,您应该尝试使用布尔值或浮点变量类型来存储数字。

答案 1 :(得分:0)

您使用的是整数而不是浮点数。整数不能有小数。

更改行 int op1; 至 float op1;

op2 = Integer.parseFloat(editText.getText()。toString()); 至  op2 = Float.parseFloat(editText.getText()。toString());

答案 2 :(得分:0)

您正在使用int,它始终返回整数。相反,您想使用double,它可以返回小数。所以,在你的变量声明中:

double op1;
double op2;
double result;

在您的运营中:

public void operation(){
if(optr.equals("+")){
    result = op1 + op2;
    editText.setText("Result : " + Double.toString(result));
}
else if(optr.equals("-")){
    result = op1 - op2;
    editText.setText("Result : " + Double.toString(result));
}
else if(optr.equals("*")){
    result = op1 * op2;
    editText.setText("Result : " + Double.toString(result));
}
else if(optr.equals("/")){
    result = op1 / op2;
    editText.setText("Result : " + Double.toString(result));
}

您必须重构某些代码,并将Integer的所有实例替换为Double