计算器逻辑错误

时间:2018-05-07 19:51:00

标签: java calculator

我正在研究一个带有java的简单计算器,我遇到了一些错误。

在我的代码中,我编写了它,以便在单击乘法按钮时,它会将前两个输入的数字相乘,但是当单击该按钮时,没有任何反应。

由于某些原因,当单击减去按钮时,它会添加已输入的第二个数字。

由于某些原因,调试器也没有多大帮助。

有人看到的东西不是吗?

int num1;
int num2;
int ans;

boolean kms = false;
boolean multiplication = false;
boolean division = false;
boolean subtract = false;
boolean addition = false;

public App() {

    times.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            txt.setText("X");
            kms = true;
            multiplication = true;

        }
    });
    div.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            txt.setText("/");
            kms = true;
            division = true;
        }
    });
    min.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            txt.setText("-");
            kms = true;
            subtract = true;
        }
    });
    plus.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            txt.setText("+");
            kms = true;
            addition = true;
        }
    });

    clear.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            ans = 0;
            num1 = 0;
            num2 = 0;
            txt .setText("");
        }
    });

    if(kms == true){

        //Second number being inputted
        num1 = Integer.parseInt(txt.getText());
        txt.setText("");

        one.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("1");
                num2 = num2 + 1;

            }
        });
        two.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("2");
                num2 = num2 + 2;
            }
        });
        three.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("3");
                num2 = num2 + 3;
            }
        });
        four.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("4");
                num2 = num2 + 4;
            }
        });
        five.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("5");
                num2 = num2 + 5;
            }
        });
        six.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("6");
                num2 = num2 + 6;
            }
        });
        seven.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("7");
                num2 = num2 + 7;
            }
        });
        eight.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("8");
                num2 = num2 + 8;
            }
        });
        nine.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("9");
                num2 = num2 + 9;
            }
        });
        zero.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("0");
                num2 = num2 + 0;
            }
        });

    }else if(kms == false){
        //First number being inputted
        one.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("1");
                num1 = num1 + 1;
            }
        });
        two.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("2");
                num1 = num1 + 2;
            }
        });
        three.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("3");
                num1 = num1 + 3;
            }
        });
        four.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("4");
                num1 = num1 + 4;
            }
        });
        five.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("5");
                num1 = num1 + 5;
            }
        });
        six.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("6");
                num1 = num1 + 6;
            }
        });
        seven.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("7");
                num1 = num1 + 7;
            }
        });
        eight.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("8");
                num1 = num1 + 8;
            }
        });
        nine.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("9");
                num1 = num1 + 9;
            }
        });
        zero.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt.setText("0");
                num1 = num1 + 0;
            }
        });


        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(multiplication == true){
                    ans = num1 * num2;
                    txt.setText("Your answer is " + ans);
                    ans = 0;
                    num1 = 0;
                    num2 = 0;
                }else if(division == true){
                    ans = num1 / num2;
                    txt.setText("Your answer is " + ans);
                    ans = 0;
                    num1 = 0;
                    num2 = 0;
                }else if(subtract == true){
                    ans = num1 - num2;
                    txt.setText("Your answer is " + ans);
                    ans = 0;
                    num1 = 0;
                    num2 = 0;
                }else if(addition == true){
                    ans = num1 + num2;
                    txt.setText("Your answer is " + ans);
                    ans = 0;
                    num1 = 0;
                    num2 = 0;
                }
            }
        });

    }
}



public static void main(String[] args) {

    JFrame frame = new JFrame("Calculator");
    frame.setBackground(Color.white);
    frame.setVisible(true);
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new App().main);
    frame.pack();
}
}

0 个答案:

没有答案