Netbeans Java GUI计算器 - 如何对超过2个数字执行计算?

时间:2015-03-10 18:32:02

标签: java parsing netbeans calculator

我在netbeans中创建了一个java计算器,它对两个数字执行简单的+ - * /操作。代码如下:

@ManagedBean
@SessionScoped
public class Calculation extends javax.swing.JFrame {

double firstNum;
double secondNum;
double thirdNum;
double result;
String operation;
String operation2;

/**
 * Creates new form Calculation
 */
public Calculation() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">  

private void initComponents() {
     // Generated code here for GUI Components....             

  // Below is my calculations and displays...
private void Btn4ActionPerformed(java.awt.event.ActionEvent evt) {                                     

   String takein;
   takein = txtDisplay.getText() + Btn4.getText();
   txtDisplay.setText(takein);        
}                                    

private void Btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                     

   String takein;
   takein = txtDisplay.getText() + Btn1.getText();
   txtDisplay.setText(takein);
}                                    

private void Btn2ActionPerformed(java.awt.event.ActionEvent evt) {                                     

   String takein;
   takein = txtDisplay.getText() + Btn2.getText();
   txtDisplay.setText(takein);
}                                    

private void Btn3ActionPerformed(java.awt.event.ActionEvent evt) {                                     

   String takein;
   takein = txtDisplay.getText() + Btn3.getText();
   txtDisplay.setText(takein);
}                                    

private void Btn5ActionPerformed(java.awt.event.ActionEvent evt) {                                     

   String takein;
   takein = txtDisplay.getText() + Btn5.getText();
   txtDisplay.setText(takein);
}                                    

private void Btn6ActionPerformed(java.awt.event.ActionEvent evt) {                                     

   String takein;
   takein = txtDisplay.getText() + Btn6.getText();
   txtDisplay.setText(takein);       
}                                    

private void Btn7ActionPerformed(java.awt.event.ActionEvent evt) {                                     

   String takein;
   takein = txtDisplay.getText() + Btn7.getText();
   txtDisplay.setText(takein);        
}                                    

private void Btn8ActionPerformed(java.awt.event.ActionEvent evt) {                                     

   String takein;
   takein = txtDisplay.getText() + Btn8.getText();
   txtDisplay.setText(takein);      
}                                    

private void Btn9ActionPerformed(java.awt.event.ActionEvent evt) {                                     

   String takein;
   takein = txtDisplay.getText() + Btn9.getText();
   txtDisplay.setText(takein);       
}                                    

private void Btn0ActionPerformed(java.awt.event.ActionEvent evt) {                                     

   String takein;
   takein = txtDisplay.getText() + Btn0.getText();
   txtDisplay.setText(takein);
}                                    

private void BtnPointActionPerformed(java.awt.event.ActionEvent evt) {                                         

   String takein;
   takein = txtDisplay.getText() + BtnPoint.getText();
   txtDisplay.setText(takein);
}                                        

private void BtnCancelActionPerformed(java.awt.event.ActionEvent evt) {                                          

   txtDisplay.setText("");        
}                                         

private void BtnPlusActionPerformed(java.awt.event.ActionEvent evt) {                                        

    firstNum = Double.parseDouble(txtDisplay.getText());
    txtDisplay.setText("+");
    operation = "+";
}                                       

private void BtnMinusActionPerformed(java.awt.event.ActionEvent evt) {                                         

    firstNum = Double.parseDouble(txtDisplay.getText());
    txtDisplay.setText("-");
    operation = "-";     
}                                        

private void BtnMultiplyActionPerformed(java.awt.event.ActionEvent evt) {                                            

    firstNum = Double.parseDouble(txtDisplay.getText());
    txtDisplay.setText("*");
    operation = "*";
}                                           

private void BtnDivideActionPerformed(java.awt.event.ActionEvent evt) {                                          

    firstNum = Double.parseDouble(txtDisplay.getText());
    txtDisplay.setText("/");
    operation = "/";      
}                                         

private void BtnPlusMinusActionPerformed(java.awt.event.ActionEvent evt) {                                             

    double operan = (Double.parseDouble(String.valueOf(txtDisplay.getText())));
    operan = operan * (-1);
    txtDisplay.setText(String.valueOf(operan));
}                                            

private void BtnEqualsActionPerformed(java.awt.event.ActionEvent evt) {                                          

    String answer;
    secondNum = Double.parseDouble(txtDisplay.getText());
    switch (operation)
    {
        case "+":
            result = firstNum + secondNum;
            answer = String.format("%.0f", result);
            txtDisplay.setText(answer);

            break;

        case "-":
            result = firstNum - secondNum;
            answer = String.format("%.0f", result);
            txtDisplay.setText(answer);

            break;

        case "*":
            result = firstNum * secondNum;
            answer = String.format("%.0f", result);
            txtDisplay.setText(answer);

            break;

        case "/":
            result = firstNum / secondNum;
            answer = String.format("%.0f", result);
            txtDisplay.setText(answer);
    }
}                                         

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    // Generated code here.. for GUI.. not really relevant.
}

// Variables declaration - do not modify  // Auto generated via designview                   
private javax.swing.JButton Btn0;
private javax.swing.JButton Btn1;
private javax.swing.JButton Btn2;
private javax.swing.JButton Btn3;
private javax.swing.JButton Btn4;
private javax.swing.JButton Btn5;
private javax.swing.JButton Btn6;
private javax.swing.JButton Btn7;
private javax.swing.JButton Btn8;
private javax.swing.JButton Btn9;
private javax.swing.JButton BtnCancel;
private javax.swing.JButton BtnDivide;
private javax.swing.JButton BtnEquals;
private javax.swing.JButton BtnMinus;
private javax.swing.JButton BtnMultiply;
private javax.swing.JButton BtnPlus;
private javax.swing.JButton BtnPlusMinus;
private javax.swing.JButton BtnPoint;
private javax.swing.JTextField txtDisplay;
// End of variables declaration                   
 }

因此,从我的代码中可以看出,它只允许2个数字输入。所以,如果我要输入2 + 2 + 2,答案仍然是4.有人能指出我是如何允许第3,第4,第5个数字等被添加进行计算的正确方向吗? 感谢。

2 个答案:

答案 0 :(得分:0)

你可以做的是当用户按下2,+,2然后如果用户再按+/-执行2 + 2 = 4的动作,现在假装为4 +/- x ......

答案 1 :(得分:0)

假设您想在按下&#39; =&#39;后立即计算表达式。按钮,我建议使用类似Stack的结构(java.util.Stack)来评估中缀操作,如下所示:

  1. 为操作符(+ - / *)保留一个堆栈,为操作数(1,2,3 ...)保留一个堆栈。
  2. 精神上为您的运营商指定优先级(例如,&#39; +&#39;和&#39; - &#39;优先级低,&#39; *&#39;和&#39; ; /&#39;高处)。
  3. 一旦&#39; =&#39;单击按钮,转到步骤4.
  4. 检查输入中的下一个标记是操作数还是操作符,并将其推入适当的堆栈。
  5. 重复步骤4,直到高优先级操作员(&#39; *&#39;或&#39; /&#39;)被推入操作员堆栈,然后转到步骤6.如果输入在a之前完成高优先级操作符被推入堆栈,转到步骤7.
  6. 在运算符后检索标记并弹出操作数和运算符堆栈。然后将运算符应用于两个操作数并将结果推回操作数堆栈。转到第5步。
  7. 由于没有保留高优先级运算符且所有输入都在其各自的堆栈中,因此从它们各自的堆栈中弹出两个操作数和一个运算符,并将运算符应用于操作数。将结果推回操作数。重复,直到只剩下1个操作数。结果就是这样。
  8. 运算符优先级背后的逻辑是通常乘法和除法发生在加法和减法之前。

    有关评估表达式问题的更严格和通用的解决方案,请参阅Shunting Yard Algorithm。上面的版本是一个非常简化的形式(我相信)。