我试图创建一个计算器,但我遇到了一些问题

时间:2013-03-16 23:20:00

标签: java calculator

我遇到的问题是,我无法弄清楚如何使+, - ,/和*按钮保存我之前输入的数字,然后在我按下=时使用该数字按钮。我也无法让=按钮识别我按下的四个按钮中的哪一个,这样当我按下 - 按钮时它会减少,而不是相乘。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Calculator extends JFrame implements ActionListener{

    JPanel row1 = new JPanel();
    JTextField display = new JTextField("0");
    Font font = new Font("Calibri", Font.PLAIN, 24);

    JPanel row2 = new JPanel();
    JButton c = new JButton("C");

    JPanel row3 = new JPanel();
    JButton num7 = new JButton("7");
    JButton num8 = new JButton("8");
    JButton num9 = new JButton("9");
    JButton divide = new JButton("/");

    JPanel row4 = new JPanel();
    JButton num4 = new JButton("4");
    JButton num5 = new JButton("5");
    JButton num6 = new JButton("6");
    JButton multiply = new JButton("*");

    JPanel row5 = new JPanel();
    JButton num1 = new JButton("1");
    JButton num2 = new JButton("2");
    JButton num3 = new JButton("3");
    JButton minus = new JButton("-");

    JPanel row6 = new JPanel();
    JButton num0 = new JButton("0");
    JButton dot = new JButton(".");
    JButton equals = new JButton("=");
    JButton plus = new JButton("+");

    public Calculator(){
    super("Calculator");
    setSize(450,275);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GridLayout layout = new GridLayout(6,1);
    setLayout(layout);

    display.setFont(font);
    display.setEnabled(false);
    GridLayout layout1 = new GridLayout(1,1);
    row1.setLayout(layout1);
    row1.add(display);
    add(row1);

    c.addActionListener(this);
    c.setFont(font);
    row2.setLayout(layout1);
    row2.add(c);
    add(row2);

    num7.setFont(font);
    num8.setFont(font);
    num9.setFont(font);
    divide.setFont(font);
    num7.addActionListener(this);
    num8.addActionListener(this);
    num9.addActionListener(this);
    divide.addActionListener(this);
    GridLayout layout2 = new GridLayout(1,4);
    row3.setLayout(layout2);
    row3.add(num7);
    row3.add(num8);
    row3.add(num9);
    row3.add(divide);
    add(row3);

    num4.setFont(font);
    num5.setFont(font);
    num6.setFont(font);
    multiply.setFont(font);
    num4.addActionListener(this);
    num5.addActionListener(this);
    num6.addActionListener(this);
    multiply.addActionListener(this);
    row4.setLayout(layout2);
    row4.add(num4);
    row4.add(num5);
    row4.add(num6);
    row4.add(multiply);
    add(row4);

    num1.setFont(font);
    num2.setFont(font);
    num3.setFont(font);
    minus.setFont(font);
    num1.addActionListener(this);
    num2.addActionListener(this);
    num3.addActionListener(this);
    minus.addActionListener(this);
    row5.setLayout(layout2);
    row5.add(num1);
    row5.add(num2);
    row5.add(num3);
    row5.add(minus);
    add(row5);

    num0.setFont(font);
    dot.setFont(font);
    equals.setFont(font);
    plus.setFont(font);
    num0.addActionListener(this);
    dot.addActionListener(this);
    equals.addActionListener(this);
    plus.addActionListener(this);
    row6.setLayout(layout2);
    row6.add(num0);
    row6.add(dot);
    row6.add(equals);
    row6.add(plus);
    add(row6);

    setVisible(true);
    }

    public void actionPerformed(ActionEvent e){
        String button = e.getActionCommand();
        if(button.equals("C")){
            display.setText("0");
        }
        if(button.equals("7")){
            int num = 7;
            int prevNum = Integer.parseInt(display.getText());
            display.setText("" + prevNum + num);
        }
        if(button.equals("8")){
            int num = 8;
            int prevNum = Integer.parseInt(display.getText());
            display.setText("" + prevNum + num);
        }
        if(button.equals("9")){
            int num = 9;
            int prevNum = Integer.parseInt(display.getText());
            display.setText("" + prevNum + num);
        }
        if(button.equals("4")){
            int num = 4;
            int prevNum = Integer.parseInt(display.getText());
            display.setText("" + prevNum + num);
        }
        if(button.equals("5")){
            int num = 5;
            int prevNum = Integer.parseInt(display.getText());
            display.setText("" + prevNum + num);
        }
        if(button.equals("6")){
            int num = 6;
            int prevNum = Integer.parseInt(display.getText());
            display.setText("" + prevNum + num);
        }
        if(button.equals("1")){
            int num = 1;
            int prevNum = Integer.parseInt(display.getText());
            display.setText("" + prevNum + num);
        }
        if(button.equals("2")){
            int num = 2;
            int prevNum = Integer.parseInt(display.getText());
            display.setText("" + prevNum + num);
        }
        if(button.equals("3")){
            int num = 3;
            int prevNum = Integer.parseInt(display.getText());
            display.setText("" + prevNum + num);
        }
        if(button.equals("0")){
            int num = 0;
            int prevNum = Integer.parseInt(display.getText());
            display.setText("" + prevNum + num);
        }
    }

    public static void main(String[] args){
        Calculator frame = new Calculator();
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用此程序中的逻辑:

package com.nkb.calci;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SimpleCalculatorActivity extends Activity {
    /** Called when the activity is first created. */
    EditText disp;
    Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bplus,bminus,binto,bby,bdot,bclear,bequals;
    float op1,op2;
    char op='\0';
    int state=1,decimalPoint=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    b0=(Button) findViewById(R.id.button0);
    b1=(Button) findViewById(R.id.button1);
    b2=(Button) findViewById(R.id.button2);
    b3=(Button) findViewById(R.id.button3);
    b4=(Button) findViewById(R.id.button4);
    b5=(Button) findViewById(R.id.button5);
    b6=(Button) findViewById(R.id.button6);
    b7=(Button) findViewById(R.id.button7);
    b8=(Button) findViewById(R.id.button8);
    b9=(Button) findViewById(R.id.button9);
    bplus=(Button) findViewById(R.id.buttonplus);
    bminus=(Button) findViewById(R.id.buttonminus);
    binto=(Button) findViewById(R.id.buttoninto);
    bby=(Button) findViewById(R.id.buttonby);
    bdot=(Button) findViewById(R.id.buttondot);
    bclear=(Button) findViewById(R.id.buttonclear);
    bequals=(Button) findViewById(R.id.buttonequals);
    disp=(EditText) findViewById(R.id.disp_result);

    b0.setOnClickListener(b0Listener);
    b1.setOnClickListener(b1Listener);
    b2.setOnClickListener(b2Listener);
    b3.setOnClickListener(b3Listener);
    b4.setOnClickListener(b4Listener);
    b5.setOnClickListener(b5Listener);
    b6.setOnClickListener(b6Listener);
    b7.setOnClickListener(b7Listener);
    b8.setOnClickListener(b8Listener);
    b9.setOnClickListener(b9Listener);
    bplus.setOnClickListener(bplusListener);
    bminus.setOnClickListener(bminusListener);
    binto.setOnClickListener(bintoListener);
    bby.setOnClickListener(bbyListener);
    bdot.setOnClickListener(bdotListener);
    bclear.setOnClickListener(bclearListener);
    bequals.setOnClickListener(bequalsListener);
    }
    private View.OnClickListener b0Listener = new View.OnClickListener() {
        public void onClick(View v) {
            disp.append("0");
        }
    };
    private View.OnClickListener b1Listener = new View.OnClickListener() {
        public void onClick(View v) {
            disp.append("1");
        }
    };
    private View.OnClickListener b2Listener = new View.OnClickListener() {
        public void onClick(View v) {
            disp.append("2");
        }
    };
    private View.OnClickListener b3Listener = new View.OnClickListener() {
        public void onClick(View v) {
            disp.append("3");
        }
    };
    private View.OnClickListener b4Listener = new View.OnClickListener() {
        public void onClick(View v) {
            disp.append("4");
        }
    };
    private View.OnClickListener b5Listener = new View.OnClickListener() {
        public void onClick(View v) {
            disp.append("5");
        }
    };
    private View.OnClickListener b6Listener = new View.OnClickListener() {
        public void onClick(View v) {
            disp.append("6");
        }
    };
    private View.OnClickListener b7Listener = new View.OnClickListener() {
        public void onClick(View v) {
            disp.append("7");
        }
    };
    private View.OnClickListener b8Listener = new View.OnClickListener() {
        public void onClick(View v) {
            disp.append("8");
        }
    };
    private View.OnClickListener b9Listener = new View.OnClickListener() {
        public void onClick(View v) {
            disp.append("9");
        }
    };
    private View.OnClickListener bplusListener = new View.OnClickListener() {
        public void onClick(View v) {
            if(disp.getText().toString().equals(""));
            else if(state==1) {
                op1=Float.parseFloat(disp.getText().toString());
                op='+';
                state++;
                disp.setText("");
            }
            else {
                op2=Float.parseFloat(disp.getText().toString());
                op1=Float.parseFloat(perform(op,op1,op2));
                disp.setText("");
                op='+';
                state++;
                decimalPoint=0;
            }
        }
    };
    private View.OnClickListener bminusListener = new View.OnClickListener() {
        public void onClick(View v) {
            if(disp.getText().toString().equals(""));
            else if(state==1) {
                op1=Float.parseFloat(disp.getText().toString());
                op='-';
                state++;
                disp.setText("");
            }
            else {
                op2=Float.parseFloat(disp.getText().toString());
                op1=Float.parseFloat(perform(op,op1,op2));
                disp.setText("");
                op='-';
                state++;
                decimalPoint=0;
            }
        }
    };
    private View.OnClickListener bintoListener = new View.OnClickListener() {
        public void onClick(View v) {
            if(disp.getText().toString().equals(""));
            else if(state==1) {
                op1=Float.parseFloat(disp.getText().toString());
                op='*';
                state++;
                disp.setText("");
            }
            else {
                op2=Float.parseFloat(disp.getText().toString());
                op1=Float.parseFloat(perform(op,op1,op2));
                disp.setText("");
                op='*';
                state++;
                decimalPoint=0;
            }
        }
    };
    private View.OnClickListener bbyListener = new View.OnClickListener() {
        public void onClick(View v) {
            if(disp.getText().toString().equals(""));
            else if(state==1) {
                op1=Float.parseFloat(disp.getText().toString());
                op='/';
                state++;
                disp.setText("");
            }
            else {
                op2=Float.parseFloat(disp.getText().toString());
                op1=Float.parseFloat(perform(op,op1,op2));
                disp.setText("");
                op='/';
                state++;
                decimalPoint=0;
            }
        }
    };
    private View.OnClickListener bdotListener = new View.OnClickListener() {
        public void onClick(View v) {
            if(!disp.getText().toString().equals("")) {
                disp.append(".");
                decimalPoint=1;
            }
        }
    };
    private View.OnClickListener bclearListener = new View.OnClickListener() {
        public void onClick(View v) {
            op='\0';
            state=1;
            disp.setText("");
            decimalPoint=0;
        }
    };
    private View.OnClickListener bequalsListener = new View.OnClickListener() {
        public void onClick(View v) {
            if(state!=1) {
                op2=Float.parseFloat(disp.getText().toString());
                disp.setText(perform(op,op1,op2));
                op='\0';
                state=1;
                decimalPoint=0;
            }
        }
    };
    String perform(char op,float op1,float op2) {
        switch(op) {
            case '+' : return Float.toString(op1+op2);
            case '-' : return Float.toString(op1-op2);
            case '*' : return Float.toString(op1*op2);
            case '/' : try {
                            return Float.toString(op1/op2);
                        }
                        catch(ArithmeticException e) {
                            return "Division by zero error...";
                        }
        }
        return null;
    }
}

这是一个Android java程序,可以满足您的需求。您可以在java程序中轻松实现逻辑。