我在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个数字等被添加进行计算的正确方向吗? 感谢。
答案 0 :(得分:0)
你可以做的是当用户按下2,+,2然后如果用户再按+/-执行2 + 2 = 4的动作,现在假装为4 +/- x ......
答案 1 :(得分:0)
假设您想在按下&#39; =&#39;后立即计算表达式。按钮,我建议使用类似Stack的结构(java.util.Stack)来评估中缀操作,如下所示:
运算符优先级背后的逻辑是通常乘法和除法发生在加法和减法之前。
有关评估表达式问题的更严格和通用的解决方案,请参阅Shunting Yard Algorithm。上面的版本是一个非常简化的形式(我相信)。