Java变量突然等于0

时间:2014-10-03 23:11:32

标签: java variables user-interface if-statement

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

突然之间,num1在第一次命中if语句时等于0。你能告诉我并向我解释原因吗?另外,请不要太技术化;我只是一个业余程序员。 我尝试了一个setter和getter方法来传输变量,但是在if语句中它会被分解。

2 个答案:

答案 0 :(得分:1)

double num1 = 0;

之后,num1永远不会再次设置。

答案 1 :(得分:1)

num1在您定义时开始为0:double num1 = 0;。如果字符串num与第一个号码的任何一个检查都不匹配,当你到达检查操作的块时,它仍然是0,因为它会保持为0,因为你没有把它分配给任何东西。

在旁注中,您可以用num1 = Integer.parseInt(n.getActionCommand(), 10);替换整个第一个if块,这会将n.getActionCommand()的字符串结果转换为一行中的int而不是巨大的if块。