我需要一些关于Java GUI计算器的帮助。
当我尝试添加5 + 2时,代码在第一次计算时工作正常,它给了我答案7。
但是当我尝试进行另一次乘法,加法,减去等计算时,它会给我错误。我不能添加任何东西来建立答案。
我知道问题,但我不知道如何修复它。
String.format("%1$,.2f", result);
这会导致问题。
抱歉我的英语不好。
double firstnumb;
double secnumb;
double result;
String operations;
String ans;
public Calculator() {
initComponents();
jTextField1.setText("");
}
private void operators () {
switch (operations) {
case "+":
result=firstnumb+secnumb;
ans=String.format("%1$,.2f", result);
jTextField1.setText(ans);
break;
case "-":
result=firstnumb-secnumb;
ans=String.format("%1$,.2f", result);
jTextField1.setText(ans);
break;
case "*":
result=firstnumb*secnumb;
ans=String.format("%1$,.2f", result);
jTextField1.setText(ans);
break;
case "/":
result=firstnumb/secnumb;
ans=String.format("%1$,.2f", result);
jTextField1.setText(ans);
break;
case "%":
result=firstnumb%secnumb;
ans=String.format("%1$,.2f", result);
jTextField1.setText(ans);break;
default: jTextField1.setText("");break;
}
}
private void BtnMultiplyActionPerformed(java.awt.event.ActionEvent evt)
{
firstnumb=Double.parseDouble(jTextField1.getText());
jTextField1.setText("");
operations="*";
jTextField2.setText(firstnumb + operations);
}
private void BtnCActionPerformed(java.awt.event.ActionEvent evt)
{
jTextField1.setText("");
jTextField2.setText("");
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText()+ "1");
}
private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText()+ "2");
}
private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText()+ "3");
}
private void jButton21ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText()+ "4");
}
private void jButton22ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText()+ "5");
}
private void jButton23ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText()+ "6");
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText()+ "7");
}
private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText()+ "8");
}
private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText()+ "9");
}
private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText()+ "0");
}
private void jButton19ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText()+ ".");
}
private void BtnCEActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("");
jTextField2.setText("");
}
private void BtnEqualActionPerformed(java.awt.event.ActionEvent evt) {
secnumb=Double.parseDouble(jTextField1.getText());
operators();
}
private void BtnAdditionActionPerformed(java.awt.event.ActionEvent evt) {
firstnumb=Double.parseDouble(jTextField1.getText());
jTextField1.setText("");
operations="+";
jTextField2.setText(firstnumb + operations);
}
private void BtnMinusActionPerformed(java.awt.event.ActionEvent evt) {
firstnumb=Double.parseDouble(jTextField1.getText());
jTextField1.setText("");
operations="-";
jTextField2.setText(firstnumb + operations);
}
private void BtnDivideActionPerformed(java.awt.event.ActionEvent evt) {
firstnumb=Double.parseDouble(jTextField1.getText());
jTextField1.setText("");
operations="/";
jTextField2.setText(firstnumb + operations);
}
private void BtnModActionPerformed(java.awt.event.ActionEvent evt) {
firstnumb=Double.parseDouble(jTextField1.getText());
jTextField1.setText("");
operations="%";
jTextField2.setText(firstnumb + operations);
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
double ops = Double.parseDouble (String.valueOf (jTextField1.getText()));
ops=ops * (-1);
jTextField1.setText(String.valueOf(ops));
}