2013-10-24 34 views
1

我在計算一個簡單的計算器應用程序中處理用戶錯誤的方法時遇到了困難。例如,如果用戶按順序按下1 + 1 =按鈕,他們將得到2.但是,如果他們不按順序按下任何按鈕,如先按+號,則應用程序會崩潰。我可以在代碼中編寫什麼內容來檢查用戶是否不首先輸入整數?這裏是我當前的代碼:在簡單的Android計算器應用程序中處理用戶錯誤

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) 
+0

「+1 + 1」,或者「+1 + +1」或者「-1 + -1」都是有效的計算。你應該重新考慮你的解析器。 – Simon

+0

感謝您的迴應,但部分問題是,無論何時用戶在敲擊Integer鍵之前首先敲擊任何操作員鍵(+, - ,*,/),應用程序都會崩潰。 – user2751164

+0

發佈您的跟蹤。 –

回答

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

這條線是導致飛機失事的原因,只是把支票空或空字符串作爲這種方法的第一件事。

if(disp.getText().toString().trim().length() <= 0) return; 
+0

這個伎倆。非常感謝你! – user2751164

+0

不客氣的朋友,開心的發展:) –

+0

op2 =(disp.getText()!= null)?Integer.parseInt(disp.getText()。toString()): - 1; –

0

我認爲這是一個簡單的計算器,爲您的學習目的。
所以我假設你的計算器支持2個操作數和一個操作符的計算。
好的,現在有些記憶變量呢?即,

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; 

你的想法展開此。歡呼聲

-1

這裏是代碼的實行點,添加它在你的代碼和享受。

case R.id.btnPoint: 
    String ButtonText = ((Button)v).getText().toString(); 
    boolean IsDot = ButtonText.equals("."); 
    if(IsDot && dotPresent) 
     return; 
    Display.setText(Display.getText().toString() + ButtonText); 
    if (IsDot) 
     dotPresent = false; 
    break; 
相關問題