Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java计算器错误_Java_Jbutton_Calculator - Fatal编程技术网

Java计算器错误

Java计算器错误,java,jbutton,calculator,Java,Jbutton,Calculator,我正在用JAVA编写一个简单的计算器,因为我正在尝试使用GUI。我遇到了一些仍然无法用调试器解决的错误 计算器上表示*、-ans等的按钮不能正常工作,唯一能正常工作的是加号按钮 除法按钮从不输出任何东西,它输出一个错误,表示不能被零除。这是最奇怪的一个,因为如果输入或保存的数字为零,肯定会影响正答案,但事实并非如此 乘法按钮输出一个零作为其答案,减号按钮将两个数字相加,而不是从另一个数字中减去一个 如果有人看到上述任何问题的原因,请告诉我 import javax.swing.*; impo

我正在用JAVA编写一个简单的计算器,因为我正在尝试使用GUI。我遇到了一些仍然无法用调试器解决的错误

计算器上表示*、-ans等的按钮不能正常工作,唯一能正常工作的是加号按钮

除法按钮从不输出任何东西,它输出一个错误,表示不能被零除。这是最奇怪的一个,因为如果输入或保存的数字为零,肯定会影响正答案,但事实并非如此

乘法按钮输出一个零作为其答案,减号按钮将两个数字相加,而不是从另一个数字中减去一个

如果有人看到上述任何问题的原因,请告诉我

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

  //Version 1.0

 public class App {
  private JLabel txt;
  private JPanel main;
  private JButton one;
  private JButton three;
  private JButton nine;
  private JButton seven;
  private JButton eight;
  private JButton two;
  private JButton four;
  private JButton six;
  private JButton clear;
  private JButton min;
  private JButton div;
  private JButton times;
  private JButton zero;
  private JButton five;
  private JButton button1;
  private JButton plus;

//Variables are declared
int num1;
int num2;
int ans;

boolean kms = false;

int input;
/* boolean multiplication = false;
boolean division = false;
boolean subtract = false;
boolean addition = false;*/

public App() {

    txt.setText("0");


    times.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            txt.setText("*");
            input = 1;
            kms = true;
        }
    });

    div.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            txt.setText("/");
            kms = true;
            input = 2;
        }
    });

    min.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            txt.setText("-");
            kms = true;
            input = 3;
        }
    });

    plus.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            txt.setText("+");
            kms = true;
            input = 4;
        }
    });

    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(txt + "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;
            }
        });

        num2 = Integer.parseInt(txt.getText());

    }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;
            }
        });

        num2 = Integer.parseInt(txt.getText());

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                switch(input){
                    case 1:
                        ans = num1 * num2;
                        txt.setText("Answer is " + ans);
                    break;
                    case 2:
                        ans = num1 / num2;
                        txt.setText("Answer is " + ans);
                    break;
                    case 3:
                        ans = num1 - num2;
                        txt.setText("Answer is " + ans);
                    break;
                    case 4:
                        ans = num1 + num2;
                        txt.setText("Answer is " + ans);
                    break;
                }
            }
        });


    }
}




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();
}

}

根据您在上述代码中所做的操作,如果您只输入一个数字的整数,它将非常有效

例如:添加5,然后
num1=5
并按
+
按钮,然后
kms
true
,然后输入6,如果现在按
结果
则输入结果 将是11岁

但是,如果您尝试在结果之前输入另一个数字,则输入的数字将添加到
num2
中,就像
num2=num2+newaddedgit
一样,而不是连接到
num2


因此,为了获得计算器的真正功能,请尝试将按下的
num1
num2

共享的数字按钮连接起来,以便我可以找到错误。这里我没有发现任何错误OK我现在正在编辑帖子,而不是同时获取num1和num2,分别获取它们,如
输入num1:
,然后输入
输入num2:
,然后进行计算亲爱的@Swize请明确说明您需要我们的帮助。是否希望将计算器逻辑作为答案发布?或者,被零除就是您所遇到的错误,您需要帮助才能解决这个问题吗?请共享完整的代码,您将快速获得帮助。谢谢!很抱歉没有解释清楚,因为我还是编程新手。没关系@Swize。很高兴这对你有帮助。但下一次,你要更准确地回答这个问题,这样你就能更有效地得到答案。