2014-09-12 194 views
2

我正在爲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; 
     } 
    } 
} 

我該如何解決這個問題?

回答

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)); 
} 

你將不得不重構你的一些代碼,並與Double取代的Integer所有實例。

+0

非常感謝現在的作品 – 2014-09-12 20:30:37

+0

另一種爲Double在這種情況下可能是[的BigDecimal(http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html)。如果你的計算器需要比基礎數學更高的準確度,請務必閱讀Double vs BigDecimal的好處。 – Compass 2014-09-12 20:50:26

0

您正在使用整數而不是浮動。整數不能有小數。

變化線等 INT OP1; 至 float op1;

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

+0

我應該用parseFloat更改parseInt嗎? – 2014-09-12 19:10:54

+0

是的,對不起,我編輯了我的帖子後不久。 – marsh 2014-09-12 19:14:16

1

Java,整數除法自動截斷任何十進制值。解決這個問題

一種方法是修改師statment op1 = op1/op2;類似於下面的內容:

op1 = op1/(double) op2

聲明Integer.parseInt()會出現問題轉換爲十進制數,因爲整數不能有小數。這可能是爲什麼當您嘗試輸入帶小數的數字時出現錯誤「不幸應用程序已停止」。

要考慮小數,你應該嘗試一個布爾值或保存數字的浮點型變量的類型。