我在 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。有人能指出我正确的方向,即如何允许添加第三个、第四个、第五个数字等进行计算吗? 谢谢。
最佳答案
假设您想在按下“=”按钮时立即计算表达式,我建议使用 Stack类似结构(java.util.Stack)用于评估中缀操作,如下所示:
- 为运算符 (+-/*) 保留一个堆栈,为操作数 (1,2,3...) 保留一个堆栈。
- 在心里为运算符指定一个优先级(例如,“+”和“-”优先级较低,“*”和“/”优先级较高)。
- 点击“=”按钮后,请转到步骤 4。
- 检查输入中的下一个标记是操作数还是运算符,并将其插入适当的堆栈。
- 重复步骤 4,直到将高优先级运算符(“*”或“/”)插入运算符堆栈,然后转到步骤 6。如果在高优先级运算符插入堆栈之前输入完成,请转到步骤 7。
- 检索运算符后的 token 并弹出操作数和运算符堆栈。然后将运算符应用于两个操作数并将结果推回操作数堆栈。转到步骤 5。
- 由于没有留下高优先级运算符,并且所有输入都在其各自的堆栈中,因此从各自的堆栈中弹出两个操作数和一个运算符,并将该运算符应用于操作数。将结果推回到操作数中。重复直到只剩下 1 个操作数。这就是结果。
运算符优先级背后的逻辑是,通常乘法和除法当然发生在加法和减法之前。
有关评估表达式问题的更严格和通用的解决方案,请参阅 Shunting Yard Algorithm 。上面的版本是相同版本的非常简化的形式(我相信)。
关于java - Netbeans Java GUI 计算器 - 如何执行 2 个以上数字的计算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28971469/