我正在尝试制作一个简单的计算器

时间:2019-05-19 16:17:56

标签: java swing

我正在尝试制作一个简单的计算器,但是在我执行第一个操作后,它会忽略第一个数字,并显示第二个。我不知道问题出在哪里,所以我将整个代码放进去,这样您就可以看到我写的内容了。我真的不知道问题出在哪里,因为我不知道如何使用ActionEvent ...

public class Problema4 extends JFrame implements ActionListener{

JLabel out,inner;
JButton plus,minus,ori,impartire,egal,curata,noua;
double operatie = 0;
int start = 0;
String rez = new String();
String operator = new String();
JButton[][] butoane = new JButton[3][3];


Problema4(){
    super("Calculator (Demo)");
    this.setSize(500,500);
    this.setLayout(new GridLayout(7,4));

     plus = new JButton("+");
    minus = new JButton("-");
     ori = new JButton("*");
    impartire = new JButton("/");
   egal = new JButton("=");
    inner = new JLabel();
    out = new JLabel();
   curata = new JButton("CE");
   noua = new JButton("9");


this.add(plus);
this.add(minus);
this.add(ori);
this.add(impartire);
this.add(egal);
this.add(curata);


plus.addActionListener(this);
minus.addActionListener(this);
ori.addActionListener(this);
impartire.addActionListener(this);
egal.addActionListener(this);
curata.addActionListener(this);
noua.addActionListener(this);

int nr = 0;
for(int i = 0 ; i < 3; i ++)
    for(int j = 0 ; j < 3;  j ++)
    {
        butoane[i][j] = new JButton(String.valueOf(nr));
        this.add(butoane[i][j]);
        butoane[i][j].addActionListener(this);
        nr++;
    }


this.add(noua);
this.add(inner);
this.add(out);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setLocationRelativeTo(null);
}
    public static void main(String[] args) {
    Problema4 ob = new Problema4();
}


public void actionPerformed(ActionEvent e) {

    int nr1 = 0,nr=0;


    if(e.getSource() == curata) {
        inner.setText("");
        out.setText("");
        operator = new String();
        operatie = 0;
        rez = new String();
        nr=0;
    }
if(e.getSource() == egal) {
        nr = 0;
        operatie = 0;
        operator = new String("a");

        for(int i = 0 ; i < rez.length(); i ++) {

                if(i == rez.length() - 1) {
                    nr=nr*10+Integer.parseInt(String.valueOf(rez.charAt(i)));

                if(operator.compareTo("+")==0)
                    operatie+=nr;
                if(operator.compareTo("-")==0)
                    operatie-=nr;
                if(operator.compareTo("*")==0)
                    operatie*=nr;
                if(operator.compareTo("/")==0)
                    operatie/=nr;

            } else {

            if(Character.isDigit(Character.valueOf(rez.charAt(i))) == true)
                nr=nr*10+Integer.parseInt(String.valueOf(rez.charAt(i)));
            else{
                if(start == 0) {
                    operatie = nr;
                    start = 1;
                    nr=0;
                }
                else {
                if(operator.compareTo("+")==0)
                    operatie+=nr;
                if(operator.compareTo("-")==0)
                    operatie-=nr;
                if(operator.compareTo("*")==0)
                    operatie*=nr;
                if(operator.compareTo("/")==0)
                    operatie/=nr;
                }
                operator = new String(String.valueOf(rez.charAt(i)));
                System.out.println(operator);
                nr = 0;
            }
        }
    }//end for

        out.setText(String.valueOf(operatie));
        rez = new String();
    }//end if pentru butonul egal

    else if(e.getSource() == plus)
        {
        operator = new String("+");
        rez+=operator;
        }
    else if(e.getSource() == minus)
        {
        operator = new String("-");
        rez+=operator;
        }
    else if(e.getSource() == ori)
        {
        operator = new String("*");
        rez+=operator;
        }
    else if(e.getSource() == impartire)
        {
        operator = new String("/");
        rez+=operator;
        }
    else { if(e.getSource() == butoane[0][0])
        nr1 = 0;
    else if(e.getSource() == butoane[0][1])
        nr1 = 1;
    else if(e.getSource() == butoane[0][2])
        nr1= 2;
    else if(e.getSource() == butoane[1][0])
        nr1 = 3;
    else if(e.getSource() == butoane[1][1])
        nr1 = 4;
    else if(e.getSource() == butoane[1][2])
        nr1 = 5;
    else if(e.getSource() == butoane[2][0])
        nr1 = 6;
    else if(e.getSource() == butoane[2][1])
        nr1 = 7;
    else if(e.getSource() ==butoane[2][2])
        nr1 = 8;
    else if(e.getSource() == noua)
        nr1 = 9;

        rez+=String.valueOf(nr1);
    }
        inner.setText(String.valueOf(rez));

}

}

1 个答案:

答案 0 :(得分:1)

我已经在本地系统上尝试过您的代码。输出输出后,您无需重新设置开始变量。

即。

 out.setText(String.valueOf(operatie));
     System.out.println(operatie);
     start = 0;    // Resetting start value here. 
     result = new String();

那之后对我来说很好。