1

我无法在一个简单的计算器应用程序中找出处理用户错误的方法。例如,如果用户按该顺序按下 1 + 1 = 按钮,他们将得到 2。但是,如果他们不按顺序按下任何按钮,例如先按下 + 号,应用程序就会崩溃。我可以在代码中写什么来检查用户是否没有先输入整数?这是我当前的代码:

package edu.wcu.captainfalculator2;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

public class Falculator extends Activity implements View.OnClickListener{

    Button one, two, three, four, five, six, seven, eight, nine, zero, add, sub, mul, div, cancel, equal, stats;
    EditText disp;
    int op1;
    int op2;
    String optr;
    SharedPreferences settings;
    SharedPreferences.Editor editor;

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

        settings = getSharedPreferences("MyPreferences",Context.MODE_PRIVATE );
        editor = settings.edit();

        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);
        add = (Button) findViewById(R.id.add);
        sub = (Button) findViewById(R.id.sub);
        mul = (Button) findViewById(R.id.mul);
        div = (Button) findViewById(R.id.div);
        cancel = (Button) findViewById(R.id.cancel);
        equal = (Button) findViewById(R.id.equal);
        stats = (Button) findViewById(R.id.stats);

        disp = (EditText) findViewById(R.id.display);
        op1 = 0;
        op2 = 0;


        try{
            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);

            cancel.setOnClickListener(this);

            add.setOnClickListener(this);

            sub.setOnClickListener(this);

            mul.setOnClickListener(this);

            div.setOnClickListener(this);

            equal.setOnClickListener(this);
        }
        catch(Exception e){

        }
    }
    public void operation(){
        if(optr.equals("+")){
            op2 = Integer.parseInt(disp.getText().toString());
            disp.setText(Integer.toString(op1));
            op1 = op1 + op2;
            disp.setText(Integer.toString(op1));
            long startedCount = settings.getLong(AppConstraints.ADDITIONS, 0l);
            startedCount++;
            editor.putLong(AppConstraints.ADDITIONS, startedCount);
            editor.commit();

        }
        else if(optr.equals("-")){
            op2 = Integer.parseInt(disp.getText().toString());
            disp.setText(Integer.toString(op1));
            op1 = op1 - op2;
            disp.setText(Integer.toString(op1));
            long startedCount = settings.getLong(AppConstraints.SUBTRACTIONS, 0l);
            startedCount++;
            editor.putLong(AppConstraints.SUBTRACTIONS, startedCount);
            editor.commit();

        }
        else if(optr.equals("*")){
            op2 = Integer.parseInt(disp.getText().toString());
            disp.setText(Integer.toString(op1));
            op1 = op1 * op2;
            disp.setText(Integer.toString(op1));
            long startedCount = settings.getLong(AppConstraints.MULTIPLICATIONS, 0l);
            startedCount++;
            editor.putLong(AppConstraints.MULTIPLICATIONS, startedCount);
            editor.commit();

        }
        else if(optr.equals("/")){
            op2 = Integer.parseInt(disp.getText().toString());
            disp.setText(Integer.toString(op1));
            op1 = op1 / op2;
            disp.setText(Integer.toString(op1));
            long startedCount = settings.getLong(AppConstraints.DIVISIONS, 0l);
            startedCount++;
            editor.putLong(AppConstraints.DIVISIONS, startedCount);
            editor.commit();

        }
    }
    @Override
    public void onClick(View arg0) {
        Editable str =  disp.getText();
        switch(arg0.getId()){
            case R.id.one:
                if(op2 != 0){
                    op2 = 0;
                    disp.setText("");
                }
                str = str.append(one.getText());
                disp.setText(str);
            break;
            case R.id.two:
                if(op2 != 0){
                    op2 = 0;
                    disp.setText("");
                }
                str = str.append(two.getText());
                disp.setText(str);
            break;
            case R.id.three:
                if(op2 != 0){
                    op2 = 0;
                    disp.setText("");
                }
                str = str.append(three.getText());
                disp.setText(str);
            break;
            case R.id.four:
                if(op2 != 0){
                    op2 = 0;
                    disp.setText("");
                }
                str = str.append(four.getText());
                disp.setText(str);
            break;
            case R.id.five:
                if(op2 != 0){
                    op2 = 0;
                    disp.setText("");
                }
                str = str.append(five.getText());
                disp.setText(str);
            break;
            case R.id.six:
                if(op2 != 0){
                    op2 = 0;
                    disp.setText("");
                }
                str = str.append(six.getText());
                disp.setText(str);
            break;
            case R.id.seven:
                if(op2 != 0){
                    op2 = 0;
                    disp.setText("");
                }
                str = str.append(seven.getText());
                disp.setText(str);
            break;
            case R.id.eight:
                if(op2 != 0){
                    op2 = 0;
                    disp.setText("");
                }
                str = str.append(eight.getText());
                disp.setText(str);

            break;
            case R.id.nine:
                if(op2 != 0){
                    op2 = 0;
                    disp.setText("");
                }
                str = str.append(nine.getText());
                disp.setText(str);
            break;
            case R.id.zero:
                if(op2 != 0){
                        op2 = 0;
                    disp.setText("");
                }
                str = str.append(zero.getText());
                disp.setText(str);
            break;
            case R.id.cancel:
                op1 = 0;
                op2 = 0;
                disp.setText("");


            break;
            case R.id.add:

            optr = "+";
            long startedCountPlus = settings.getLong(AppConstraints.ADDITIONS, 0l);
            startedCountPlus++;
            editor.putLong(AppConstraints.ADDITIONS, startedCountPlus);
            editor.commit();
            if(op1 == 0){
                op1 = Integer.parseInt(disp.getText().toString());
                disp.setText("");
            }
            else if(op2 != 0){
                op2 = 0;
                disp.setText("");
            }
            else{
                op2 = Integer.parseInt(disp.getText().toString());
                disp.setText("");
                op1 = op1 + op2;
                disp.setText(Integer.toString(op1));
            }


        break;
        case R.id.sub:
            optr = "-";
            long startedCountMinus = settings.getLong(AppConstraints.SUBTRACTIONS, 0l);
            startedCountMinus++;
            editor.putLong(AppConstraints.SUBTRACTIONS, startedCountMinus);
            editor.commit();
            if(op1 == 0){
                op1 = Integer.parseInt(disp.getText().toString());
                disp.setText("");
            }
            else if(op2 != 0){
                op2 = 0;
                disp.setText("");
            }
            else{
                op2 = Integer.parseInt(disp.getText().toString());
                disp.setText("");
                op1 = op1 - op2;
                disp.setText(Integer.toString(op1));
            }
        break;
        case R.id.mul:
            optr = "*";
            long startedCountMul = settings.getLong(AppConstraints.MULTIPLICATIONS, 0l);
            startedCountMul++;
            editor.putLong(AppConstraints.MULTIPLICATIONS, startedCountMul);
            editor.commit();
            if(op1 == 0){
                op1 = Integer.parseInt(disp.getText().toString());
                disp.setText("");
            }
            else if(op2 != 0){
                op2 = 0;
                disp.setText("");
            }
            else{
                op2 = Integer.parseInt(disp.getText().toString());
                disp.setText("");
                op1 = op1 * op2;
                disp.setText(Integer.toString(op1));
            }
        break;
        case R.id.div:
            optr = "/";
            long startedCountDiv = settings.getLong(AppConstraints.DIVISIONS, 0l);
            startedCountDiv++;
            editor.putLong(AppConstraints.DIVISIONS, startedCountDiv);
            editor.commit();
            if(op1 == 0){
                op1 = Integer.parseInt(disp.getText().toString());
                disp.setText("");
            }
            else if(op2 != 0){
                op2 = 0;
                disp.setText("");
            }
            else{
                op2 = Integer.parseInt(disp.getText().toString());
                disp.setText("");
                op1 = op1 / op2;
                disp.setText(Integer.toString(op1));
            }
        break;
        case R.id.equal:
            if(!optr.equals(null)){
                long startedCountEqu = settings.getLong(AppConstraints.CALCULATIONS, 0l);
                startedCountEqu++;
                editor.putLong(AppConstraints.CALCULATIONS, startedCountEqu);
                editor.commit();
                if(op2 != 0){
                    if(optr.equals("+")){
                        disp.setText("");

                        disp.setText(Integer.toString(op1));
                    }
                    else if(optr.equals("-")){
                        disp.setText("");

                        disp.setText(Integer.toString(op1));
                    }
                    else if(optr.equals("*")){
                        disp.setText("");

                        disp.setText(Integer.toString(op1));
                    }
                    else if(optr.equals("/")){
                        disp.setText("");

                        disp.setText(Integer.toString(op1));
                    }
                }
                else{
                    operation();
                }
            }
        break;
        case R.id.stats:
            Intent next = new Intent(this, edu.wcu.captainfalculator2.Statistics.class);
            this.startActivity(next);
        break;
    }
}

}

编辑:这是堆栈跟踪

10-24 14:38:10.046: E/AndroidRuntime(9732): FATAL EXCEPTION: main
10-24 14:38:10.046: E/AndroidRuntime(9732): java.lang.NumberFormatException: unable to parse '' as integer
10-24 14:38:10.046: E/AndroidRuntime(9732):     at java.lang.Integer.parseInt(Integer.java:362)
10-24 14:38:10.046: E/AndroidRuntime(9732):     at java.lang.Integer.parseInt(Integer.java:332)
10-24 14:38:10.046: E/AndroidRuntime(9732):     at edu.wcu.captainfalculator2.Falculator.onClick(Falculator.java:237)
10-24 14:38:10.046: E/AndroidRuntime(9732):     at android.view.View.performClick(View.java:2538)
10-24 14:38:10.046: E/AndroidRuntime(9732):     at android.view.View$PerformClick.run(View.java:9152)
10-24 14:38:10.046: E/AndroidRuntime(9732):     at android.os.Handler.handleCallback(Handler.java:587)
10-24 14:38:10.046: E/AndroidRuntime(9732):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 14:38:10.046: E/AndroidRuntime(9732):     at android.os.Looper.loop(Looper.java:130)
10-24 14:38:10.046: E/AndroidRuntime(9732):     at android.app.ActivityThread.main(ActivityThread.java:3687)
10-24 14:38:10.046: E/AndroidRuntime(9732):     at java.lang.reflect.Method.invokeNative(Native Method)
10-24 14:38:10.046: E/AndroidRuntime(9732):     at java.lang.reflect.Method.invoke(Method.java:507)
10-24 14:38:10.046: E/AndroidRuntime(9732):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
10-24 14:38:10.046: E/AndroidRuntime(9732):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-24 14:38:10.046: E/AndroidRuntime(9732):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1
        op2 = Integer.parseInt(disp.getText().toString());

此行导致崩溃,只需在此方法中首先检查 null 或空字符串。

if(disp.getText().toString().trim().length() <= 0) return;
于 2013-10-24T18:40:46.577 回答
0

我认为这是一个用于学习目的的简单计算器。
所以我假设你的计算器支持 2 个操作数和一个运算符的计算。
好的,现在一些记忆变量呢?IE,

Boolean oper1=false,oper2=false,operator=false;

现在在整数按钮里面点击,

if(!oper1)
 oper1=true;
else if(operator)
 oper2=true;

并在操作员按钮内单击,

if(!oper1)
 // prompt user for first operand
else
 operator=true;

用你的想法扩展它。干杯

于 2013-10-24T18:44:33.173 回答