【发布时间】:2018-05-07 19:51:00
【问题描述】:
我正在使用 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();
}
}
【问题讨论】:
-
现在是学习如何使用调试器的好时机。真的
-
你以为我没试过?
-
@Swize 好像你已经问过同一个问题两次了。我将解决下面的问题,以帮助其他将来参考这篇文章的人。
标签: java calculator