2014-10-03 97 views
-2
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

+0

行'double num1 = 0,num2 = 0;'給你一個提示嗎? – clcto 2014-10-03 23:13:57

+0

因爲'double num1 = 0' – drewmoore 2014-10-03 23:14:07

+1

請查閱'Integer.parseInt()。' – user949300 2014-10-03 23:17:49

回答

1
double num1 = 0; 

之後,num1永遠不會再次設置。

+0

當然,在檢查第一個數字時,如果'n.getActionCommand()'的結果是一個數字。 – Krease 2014-10-03 23:25:02

1

num1當您定義它時,將從0開始:double num1 = 0;。如果字符串num與您的第一個數字的任何檢查都不匹配,那麼當您到達檢查操作的塊時它仍然爲0,它將保留爲0,因爲您不將它分配給任何內容。

在附註上,你可以用num1 = Integer.parseInt(n.getActionCommand(), 10);代替你的整個第一個塊,它將n.getActionCommand()的字符串結果轉換爲一行中的int而不是巨大的if塊。