简体   繁体   中英

I am trying to make a calculator, a simple one

I am trying to make a simple calculator, but after i run the first the first operation it is ignoring the first number and it display the second one. I don't know where the problem is so i will put the entire code, so you cand see what i wrote. I really don't know where can be the problem, because i don't know how to work with 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));

}

}

I have tried your code at my local system. You are not resetting the start variable once the output is printed.

ie.

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

It is working fine for me after that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM