Java变量突然等于0
问题描述:
public class Calculator extends JFrame{
JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, equal;
JButton plus, minus, times, divide, root, trig, factorial, square, log, cube, ln; // trig button -> next line var.
JButton sin, cos, tan, arcsin, arccos, arctan, sinh, cosh, tanh;
JLabel answer;
public Calculator() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
answer = new JLabel("");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
add(answer, gbc);
gbc.gridwidth = 3;
cube = new JButton("^3");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0; // right
gbc.gridy = 1; // down
gbc.gridwidth = 1;
add(cube, gbc);
factorial = new JButton("!");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 1;
add(factorial, gbc);
trig = new JButton("="); // expansion button
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 2;
gbc.gridy = 1;
add(trig, gbc);
log = new JButton("log");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 2;
add(log, gbc);
ln = new JButton("ln");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 2;
add(ln, gbc);
root = new JButton("sqrt()");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 2;
gbc.gridy = 2;
add(root, gbc);
square = new JButton("^2");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 3;
add(square, gbc);
plus = new JButton("+");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 3;
add(plus, gbc);
minus = new JButton("-");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 2;
gbc.gridy = 3;
add(minus, gbc);
times = new JButton("*");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 4;
add(times, gbc);
divide = new JButton("/");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 4;
add(divide, gbc);
equal = new JButton("="); // gives answer
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 2;
gbc.gridy = 4;
add(equal, gbc);
button1 = new JButton("1");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 5;
add(button1, gbc);
button2 = new JButton("2");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 5;
add(button2, gbc);
button3 = new JButton("3");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 2;
gbc.gridy = 5;
add(button3, gbc);
button4 = new JButton("4");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 6;
add(button4, gbc);
button5 = new JButton("5");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 6;
add(button5, gbc);
button6 = new JButton("6");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 2;
gbc.gridy = 6;
add(button6, gbc);
button7 = new JButton("7");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 7;
add(button7, gbc);
button8 = new JButton("8");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 7;
add(button8, gbc);
button9 = new JButton("9");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 2;
gbc.gridy = 7;
add(button9, gbc);
event n = new event();
button1.addActionListener(n);
button2.addActionListener(n);
button3.addActionListener(n);
button4.addActionListener(n);
button5.addActionListener(n);
button6.addActionListener(n);
button7.addActionListener(n);
button8.addActionListener(n);
button9.addActionListener(n);
cube.addActionListener(n);;
factorial.addActionListener(n);
trig.addActionListener(n);
log.addActionListener(n);
ln.addActionListener(n);
root.addActionListener(n);
square.addActionListener(n);
plus.addActionListener(n);
minus.addActionListener(n);
times.addActionListener(n);
divide.addActionListener(n);
equal.addActionListener(n);
}
public class event implements ActionListener {
public void actionPerformed(ActionEvent n) {
double num1 = 0, num2 = 0;
double solution = 0;
String num = n.getActionCommand();
//First number
if(num.equals("1")) {
num1 = 1;
} else if(num.equals("2")) {
num1 = 2;
} else if(num.equals("3")) {
num1 = 3;
} else if(num.equals("4")) {
num1 = 4;
} else if(num.equals("5")) {
num1 = 5;
} else if(num.equals("6")) {
num1 = 6;
} else if(num.equals("7")) {
num1 = 7;
} else if(num.equals("8")) {
num1 = 8;
} else if(num.equals("9")) {
num1 = 9;
}
answer.setText(num);
//Operations
num = n.getActionCommand();
if(num.equals("^3")) {
solution = num1 * num1 * num1;
answer.setText(num1 + "^3" + "=" + solution);
} else if(num.equals("!")) {
for(double i = num1; i > 0; i--) {
solution = solution * i;
answer.setText(num1 + "!" + "=" + solution);
}
}
所有的突然,当它第一次碰到if语句时num1等于0。你能告诉我并向我解释为什么?另外,请不要太技术性;我只是一个业余程序员。 我试过了一个setter和getter方法来传递变量,但是它在if语句里面一次就分解了。Java变量突然等于0
答
double num1 = 0;
之后,num1
永远不会再次设置。
+0
当然,在检查第一个数字时,如果'n.getActionCommand()'的结果是一个数字。 – Krease 2014-10-03 23:25:02
答
num1
当您定义它时,将从0开始:double num1 = 0;
。如果字符串num
与您的第一个数字的任何检查都不匹配,那么当您到达检查操作的块时它仍然为0,它将保留为0,因为您不将它分配给任何内容。
在附注上,你可以用num1 = Integer.parseInt(n.getActionCommand(), 10);
代替你的整个第一个块,它将n.getActionCommand()
的字符串结果转换为一行中的int而不是巨大的if块。
行'double num1 = 0,num2 = 0;'给你一个提示吗? – clcto 2014-10-03 23:13:57
因为'double num1 = 0' – drewmoore 2014-10-03 23:14:07
请查阅'Integer.parseInt()。' – user949300 2014-10-03 23:17:49