簡體   English   中英

我正在嘗試制作一個簡單的計算器

[英]I am trying to make a calculator, a simple one

我正在嘗試制作一個簡單的計算器,但是在第一個操作中運行第一個之后,它會忽略第一個數字,並顯示第二個。 我不知道問題出在哪里,所以我將整個代碼放進去,這樣您就可以看到我寫的內容。 我真的不知道問題出在哪里,因為我不知道如何使用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));

}

}

我已經在本地系統上嘗試過您的代碼。 輸出輸出后,您無需重新設置開始變量。

即。

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

在那之后對我來說很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM