到目前为止,我在netbeans中制作了一个计算器,我添加了加号和减号按钮。但是,让我说我做5 + 5它给了我10,如果我再加5,它给15.这样的部分正确。但是,如果我做9-5它给了我-4,如果我从-4中减去2我得到正6.请帮助,我是相当新的java。
public class Calculator extends javax.swing.JFrame {
/**
* Creates new form Calculator
*/
public Calculator() {
initComponents();
}
double fNums = 0.0;
double sNums = 0.0;
boolean add = false;
boolean sub = false;
/**
* 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() {
jPanel1 = new javax.swing.JPanel();
display = new javax.swing.JTextField();
seven = new javax.swing.JButton();
eight = new javax.swing.JButton();
nine = new javax.swing.JButton();
plus = new javax.swing.JButton();
four = new javax.swing.JButton();
five = new javax.swing.JButton();
six = new javax.swing.JButton();
minus = new javax.swing.JButton();
one = new javax.swing.JButton();
two = new javax.swing.JButton();
three = new javax.swing.JButton();
multiply = new javax.swing.JButton();
zero = new javax.swing.JButton();
decimal = new javax.swing.JButton();
divide = new javax.swing.JButton();
enter = new javax.swing.JButton();
clear = new javax.swing.JButton();
jMenuBar2 = new javax.swing.JMenuBar();
jMenu4 = new javax.swing.JMenu();
Home = new javax.swing.JMenuItem();
jMenu5 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
display.setEditable(false);
seven.setText("7");
seven.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sevenActionPerformed(evt);
}
});
eight.setText("8");
eight.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
eightActionPerformed(evt);
}
});
nine.setText("9");
nine.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nineActionPerformed(evt);
}
});
plus.setText("+");
plus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
plusActionPerformed(evt);
}
});
four.setText("4");
four.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fourActionPerformed(evt);
}
});
five.setText("5");
five.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fiveActionPerformed(evt);
}
});
six.setText("6");
six.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sixActionPerformed(evt);
}
});
minus.setText("-");
minus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
minusActionPerformed(evt);
}
});
one.setText("1");
one.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
oneActionPerformed(evt);
}
});
two.setText("2");
two.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
twoActionPerformed(evt);
}
});
three.setText("3");
three.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
threeActionPerformed(evt);
}
});
multiply.setText("*");
multiply.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
multiplyActionPerformed(evt);
}
});
zero.setText("0");
zero.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
zeroActionPerformed(evt);
}
});
decimal.setText(".");
decimal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
decimalActionPerformed(evt);
}
});
divide.setText("/");
divide.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
divideActionPerformed(evt);
}
});
enter.setText("Enter");
enter.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
enterActionPerformed(evt);
}
});
clear.setText("C");
clear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(enter, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(one, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(two, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(three, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(multiply, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(four, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(five, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(six, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(minus, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(display)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(seven, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(eight, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(nine, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(plus, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(zero, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(decimal, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(divide, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(display, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(seven, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(eight, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(nine, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(plus, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(four, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(five, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(six, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(minus, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(one, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(two, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(three, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(multiply, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(zero, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(decimal, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(divide, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(enter, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(54, Short.MAX_VALUE))
);
jMenu4.setText("File");
Home.setText("Home");
Home.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
HomeActionPerformed(evt);
}
});
jMenu4.add(Home);
jMenuBar2.add(jMenu4);
jMenu5.setText("Edit");
jMenuBar2.add(jMenu5);
setJMenuBar(jMenuBar2);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void HomeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Menu m = new Menu();
m.setVisible(true);
}
private void sixActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + six.getText());
}
private void divideActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + divide.getText());
}
private void sevenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + seven.getText());
}
private void eightActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + eight.getText());
}
private void nineActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + nine.getText());
}
private void plusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
fNums = fNums + Double.parseDouble(display.getText());
display.setText("");
add = true;
}
private void fourActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + four.getText());
}
private void fiveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + five.getText());
}
private void minusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
fNums = fNums - Double.parseDouble(display.getText());
display.setText("");
sub = true;
}
private void oneActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + one.getText());
}
private void twoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + two.getText());
}
private void threeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + three.getText());
}
private void multiplyActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + multiply.getText());
}
private void zeroActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + zero.getText());
}
private void decimalActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText(display.getText() + decimal.getText());
}
private void enterActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your enter code here:
if(add = true){
sNums = fNums + Double.parseDouble(display.getText());
display.setText(String.valueOf(sNums));
fNums = 0;
add = false;
} else {
if(sub = true){
sNums = fNums + Double.parseDouble(display.getText());
display.setText(String.valueOf(sNums));
fNums = 0;
sub = false;
}
}
}
private void clearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText("");
fNums = 0;
sNums = 0;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Calculator().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem Home;
private javax.swing.JButton clear;
private javax.swing.JButton decimal;
private javax.swing.JTextField display;
private javax.swing.JButton divide;
private javax.swing.JButton eight;
private javax.swing.JButton enter;
private javax.swing.JButton five;
private javax.swing.JButton four;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenu jMenu5;
private javax.swing.JMenuBar jMenuBar2;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton minus;
private javax.swing.JButton multiply;
private javax.swing.JButton nine;
private javax.swing.JButton one;
private javax.swing.JButton plus;
private javax.swing.JButton seven;
private javax.swing.JButton six;
private javax.swing.JButton three;
private javax.swing.JButton two;
private javax.swing.JButton zero;
// End of variables declaration
}
答案 0 :(得分:1)
if(add == true){
sNums = fNums + Double.parseDouble(display.getText());
display.setText(String.valueOf(sNums));
fNums = 0;
add = false;
} else {
`
`if(sub == true){
sNums = fNums + Double.parseDouble(display.getText());
display.setText(String.valueOf(sNums));
fNums = 0;
sub = false;
}
}
答案 1 :(得分:1)
首先:你错过了enterActionPerformed中的==。使用两个=符号。
第二:如果我理解正确,每当有' - '时,数字就会被否定。在编写第一个数字后会发生这种情况。
9-5给你4,因为你否定了第一个数字并将其添加到第二个数字: (-9)+5 = -4
错误在以下代码中,当您按下“ - ”按钮时,您正在执行0-valueEntered。
private void minusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
fNums = fNums - Double.parseDouble(display.getText());
display.setText("");
sub = true;
}
答案 2 :(得分:0)
您可以从Github查看我的MathExpressionEvaluator存储库,因为它可以轻松地评估简单的数学表达式。 您可以从计算器中传递一个字符串表达式,并显示您从中轻松获得的答案。
答案 3 :(得分:0)
将减号监听器更改为
private void minusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
fNums = Double.parseDouble(display.getText()); <----------
display.setText("");
sub = true;
}
并输入以
执行的操作private void enterActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your enter code here:
if(add == true){
sNums = fNums + Double.parseDouble(display.getText());
display.setText(String.valueOf(sNums));
fNums = 0;
add = false;
} else {
if(sub == true){
sNums = fNums - Double.parseDouble(display.getText()); <----------
display.setText(String.valueOf(sNums));
fNums = 0;
sub = false;
}
}
}
并检查