Netbeans Java GUI 计算器 - 如何计算两个以上的数字?

Netbeans Java GUI calculator - How to perform calculations for more than 2 numbers?

我在 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 、 + 、 2 然后如果用户再次按下 +/- 执行 2+2=4 的操作,现在假装为 4+/-x ...

假设您想在按下“=”按钮时立即计算所有表达式,我建议使用 Stack 之类的结构 (java.util.Stack) 来评估中缀操作,如下所示:

  1. 为运算符 (+-/*) 保留一个堆栈,为操作数 (1,2,3...) 保留一个堆栈。
  2. 在心里为您的运算符分配优先级(例如,“+”和“-”的优先级较低,“*”和“/”的优先级较高)。
  3. 单击“=”按钮后,转到第 4 步。
  4. 检查输入的下一个标记是操作数还是运算符,并将其推入适当的堆栈。
  5. 重复步骤 4,直到将高优先级运算符('*' 或 '/')压入运算符堆栈,然后转到步骤 6。如果在将高优先级运算符压入堆栈之前输入完成,则转到到第 7 步。
  6. 取回运算符后的记号,弹出操作数和运算符栈。然后将运算符应用于两个操作数并将结果推回操作数堆栈。转到步骤 5。
  7. 由于没有高优先级的运算符,所有输入都在各自的堆栈中,从各自的堆栈中弹出两个操作数和一个运算符,并将运算符应用于操作数。将结果推回操作数。重复直到只剩下 1 个操作数。这就是结果。

运算符优先级背后的逻辑是,通常乘法和除法当然先于加法和减法发生。

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