我正在尝试构建一个简单的计算器

时间:2014-07-18 16:12:06

标签: java android

我正在开发android简单计算器它工作正常但只有一个问题当我再次按0时它结果00000000它应该只有一个0我的活动文件是:

package com.example.droidcalc;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;
import android.widget.EditText;

    enter code here

public class MainActivity extends ActionBarActivity {

    int first,second,result;
    char operation;
    EditText disp;


    boolean newValue = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        disp = (EditText) findViewById(R.id.editText1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void button1(View sender){
        if(newValue == true)
            disp.setText("1");
        else
            disp.setText(disp.getText().toString() +  "1");

        newValue = false;   
        }
    public void button2(View sender){
        if(newValue == true)
            disp.setText("2");
        else
            disp.setText(disp.getText().toString() +  "2");

        newValue = false;
    }
    public void button3(View sender){
        if(newValue == true)
            disp.setText("3");
        else
            disp.setText(disp.getText().toString() +  "3");

        newValue = false;
    }
    public void button4(View sender){
        if(newValue == true)
            disp.setText("4");
        else
            disp.setText(disp.getText().toString() +  "4");

        newValue = false;
    }
    public void button5(View sender){
        if(newValue == true)
            disp.setText("5");
        else
            disp.setText(disp.getText().toString() +  "5");

        newValue = false;
    }
    public void button6(View sender){
        if(newValue == true)
            disp.setText("6");
        else
            disp.setText(disp.getText().toString() +  "6");

        newValue = false;
    }
    public void button7(View sender){
        if(newValue == true)
            disp.setText("7");
        else
            disp.setText(disp.getText().toString() +  "7");

        newValue = false;
    }
    public void button8(View sender){
        if(newValue == true)
            disp.setText("8");
        else
            disp.setText(disp.getText().toString() +  "8");

        newValue = false;
    }
    public void button9(View sender){
        if(newValue == true)
            disp.setText("9");
        else
            disp.setText(disp.getText().toString() +  "9");

        newValue = false;
    }
    public void button0(View sender){
        /*if(newValue == true)
            disp.setText("0");
        else
            disp.setText(disp.getText().toString() + "0");

        newValue = false;*/

        disp.append("0");
        //newValue = true;
    }
public void add(View sender){

        first = Integer.parseInt(disp.getText().toString());
        disp.setText("0");
        operation = '+'; 
        newValue=true;
    }

    public void sub(View sender){

        first = Integer.parseInt(disp.getText().toString());
        disp.setText("0");
        operation = '-'; 
        newValue=true;
    }
    public void mul(View sender){

        first = Integer.parseInt(disp.getText().toString());
        disp.setText("0");
        operation = '*'; 
        newValue=true;
    }
    public void div(View sender){

        first = Integer.parseInt(disp.getText().toString());
        disp.setText("0");
        operation = '/';  
        newValue=true;
    }
    public void equal(View sender){

        second = Integer.parseInt(disp.getText().toString());

         switch (operation){
         case '+':
             result = first + second;
             disp.setText(Integer.toString(result));
             break;

         case '-':
             result = first - second;
             disp.setText(Integer.toString(result));
             break;

         case '*':
             result = first * second;
             disp.setText(Integer.toString(result));
             break;

         case '/':
             result = first / second;
             disp.setText(Integer.toString(result));
             break;

         }

    }
    public void clr(View sender){

        disp.setText("0");
        newValue=true;
        first = 0;
        result = 0;
    }
}

1 个答案:

答案 0 :(得分:1)

你可以跟踪你的第一个号码,检查它是否为零,然后什么也不做。

if(newValue == true)
    disp.setText("0");
else
{
    if (!firstNumber.equals("0")
        disp.setText(disp.getText().toString() + "0");
    newValue = false;
}