Android中的计算器多个操作数

时间:2013-06-25 05:38:31

标签: java android calculator

我在android中设计一个计算器。如果我做例如5 + 5然后点击=,我得到10.然而,如果我做5 + 5 + 5然后点击=,我仍然得到10.我希望我的计算器处理多个操作数,以便当我做例如5 + 5 + 5,当我达到等于时,我得到15。 这是我到目前为止所做的:

package edu.uwstout.pocketcalculator;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Basic extends Activity {

    private Button one, two, three, four, 
        five, six, seven, eight, nine, zero, 
        add, subtract, multiply, divide, decimal, 
        negative, enter, clear;
    private TextView output;

    double numberOne;
    double numberTwo;
    double total;
    double plusMinus;

    int plus;
    int minus;
    int multiply_counter;
    int divide_counter;
    int decimal_counter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_basic);

        one = (Button) findViewById(R.id.b1);
        one.setTextColor(Color.BLACK);
        one.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "1");
            }
        });

        two = (Button) findViewById(R.id.b2);
        two.setTextColor(Color.BLACK);
        two.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "2");
            }
        });

        three = (Button) findViewById(R.id.b3);
        three.setTextColor(Color.BLACK);
        three.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "3");
            }
        });

        four = (Button) findViewById(R.id.b4);
        four.setTextColor(Color.BLACK);
        four.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "4");
            }
        });

        five = (Button) findViewById(R.id.b5);
        five.setTextColor(Color.BLACK);
        five.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "5");
            }
        });

        six = (Button) findViewById(R.id.b6);
        six.setTextColor(Color.BLACK);
        six.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "6");
            }
        });

        seven = (Button) findViewById(R.id.b7);
        seven.setTextColor(Color.BLACK);
        seven.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "7");
            }
        });

        eight = (Button) findViewById(R.id.b8);
        eight.setTextColor(Color.BLACK);
        eight.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "8");
            }
        });

        nine = (Button) findViewById(R.id.b9);
        nine.setTextColor(Color.BLACK);
        nine.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "9");
            }
        });

        zero = (Button) findViewById(R.id.b0);
        zero.setTextColor(Color.BLACK);
        zero.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "9");
            }
        });

        decimal = (Button) findViewById(R.id.bDot);
        decimal.setTextColor(Color.BLACK);
        decimal.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(decimal_counter == 0){
                    output.setText(output.getText() + ".");
                }
                decimal_counter = 1;
            }
        });

        negative = (Button) findViewById(R.id.bNeg);
        negative.setTextColor(Color.BLACK);
        negative.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                plusMinus=(Double.parseDouble(String.valueOf(output.getText())));
                plusMinus=plusMinus*(-1);
                output.setText(String.valueOf(plusMinus));
            }
        });

        add = (Button) findViewById(R.id.bPlus);
        add.setTextColor(Color.BLACK);
        add.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                numberOne=(Double.parseDouble(String.valueOf(output.getText())));
                plus = 1;
                output.setText("");
                decimal_counter=0;
            }
        });

        subtract = (Button) findViewById(R.id.bMinus);
        subtract.setTextColor(Color.BLACK);
        subtract.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                numberOne=(Double.parseDouble(String.valueOf(output.getText())));
                output.setText("");
                minus=1;
                decimal_counter=0;
            }
        });

        multiply = (Button) findViewById(R.id.bTimes);
        multiply.setTextColor(Color.BLACK);
        multiply.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                numberOne=(Double.parseDouble(String.valueOf(output.getText())));
                output.setText("");
                multiply_counter=1;
                decimal_counter=0;
            }
        });

        divide = (Button) findViewById(R.id.bDivide);
        divide.setTextColor(Color.BLACK);
        divide.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                numberOne=(Double.parseDouble(String.valueOf(output.getText())));
                output.setText("");
                divide_counter=1;
                decimal_counter=0;
            }
        });

        enter = (Button) findViewById(R.id.bEnter);
        enter.setTextColor(Color.BLACK);
        enter.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                output.setText(String.valueOf(enter(numberOne)));
            }
        });

        clear = (Button) findViewById(R.id.bClear);
        clear.setTextColor(Color.BLACK);
        clear.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                output.setText("");
                decimal_counter=0;
            }
        });

        output = (TextView) findViewById(R.id.bOutput);

        registerForContextMenu(output); 

    }

    public double enter(double numberOne){
        numberTwo = (Double.parseDouble(String.valueOf(output.getText())));
         if(plus>0){
             total = numberOne + numberTwo;
             numberOne = 0;
             numberTwo = 0;
             plus = 0;
             return total;
         }

         if(minus>0){
             total = numberOne - numberTwo;
             numberOne = 0;
             numberTwo = 0;
             minus = 0;
             return total;
         }

         if(multiply_counter>0){
             total = numberOne * numberTwo;
             numberOne = 0;
             numberTwo = 0;
             multiply_counter = 0;
             return total;
         }

         if(divide_counter>0){
             total = numberOne / numberTwo;
             numberOne = 0;
             numberTwo = 0;
             divide_counter = 0;
             return total;
        }
        return total;
    }
}

1 个答案:

答案 0 :(得分:2)

每次点击操作数时,您似乎都在替换numberOne ...

所以尝试在每次operand点击后执行计算...检查numberOne是否包含值,如果它包含执行操作并将值存储到numberOne ... 否则只需将值放到numberOne而不执行计算..