问题描述
- 我用java写了一个简易计算器的程序,但是有时候结果不正确
-
就是有时候计算结果后面会显示很多9或者0 ,比如计算8.3-2.1时,请问大神怎么解决啊?
我是菜菜鸟,程序如下:
//Calculator.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame
{
private JPanel p1=new JPanel();
private JButton numberButton[]=new JButton[10];
private String buttonName[]={"1","2","3","4","5","6","7","8","9","0"};
private JButton plusButton;
private JButton subButton;
private JButton mulButton;
private JButton divButton;
private JButton pointButton;
private JButton calButton;
private JTextField tf=new JTextField("");
private String add1="";
private String add2="";
private String output="";
private double Result;
private boolean Op=false;
private boolean OpClicked=false;
private int Option=0;
public keshihua2(){
super("简易计算器");
getContentPane().setLayout(new BorderLayout());
p1.setLayout(new GridLayout(4,4,10,10));
p1.setBounds(10,10,230,120);
for(int i=0;i<10;i++)
{numberButton[i]=new JButton(buttonName[i]);
numberButton[i].addActionListener(new Number());
p1.add(numberButton[i]);
}
plusButton=new JButton("+");
plusButton.addActionListener(new Plus());
p1.add(plusButton);subButton=new JButton("-"); subButton.addActionListener(new sub()); p1.add(subButton); mulButton=new JButton("*"); mulButton.addActionListener(new mul()); p1.add(mulButton); divButton=new JButton("/"); divButton.addActionListener(new div()); p1.add(divButton); pointButton=new JButton("."); pointButton.addActionListener(new point()); p1.add(pointButton); calButton=new JButton("="); calButton.addActionListener(new Cal()); p1.add(calButton); getContentPane().add(p1,BorderLayout.CENTER); tf.setBounds(10,150,230,20); tf.setHorizontalAlignment(JTextField.RIGHT); getContentPane().add(tf,BorderLayout.NORTH); validate(); setSize(240,200); setLocation(400,200); setResizable(false); setVisible(true); } class Number implements ActionListener //数字键的监听 { public void actionPerformed(ActionEvent e) { if(!OpClicked) { add1+=e.getActionCommand(); output=""+add1; tf.setText(output); } else { add2+=e.getActionCommand(); output+=e.getActionCommand(); tf.setText(output); } } } class Plus implements ActionListener //加号的监听 { public void actionPerformed(ActionEvent e) { OpClicked=!OpClicked; Op=!Op; Option=1; output+=" + "; tf.setText(output); } } class sub implements ActionListener //减号的监听 { public void actionPerformed(ActionEvent e) { OpClicked=!OpClicked; Op=!Op; Option=2; output+=" - "; tf.setText(output); } } class mul implements ActionListener //乘号的监听 { public void actionPerformed(ActionEvent e) { OpClicked=!OpClicked; Op=!Op; Option=3; output+=" * "; tf.setText(output); } } class div implements ActionListener //除号的监听 { public void actionPerformed(ActionEvent e) { OpClicked=!OpClicked; Op=!Op; Option=4; output+=" / "; tf.setText(output); } } class point implements ActionListener //小数点的监听 { public void actionPerformed(ActionEvent e) { if(!Op){ add1+="."; } else{ add2+="."; output+="."; } } } class Cal implements ActionListener //等号的监听 { public void actionPerformed(ActionEvent e) {switch(Option){ case 1: Result=Double.parseDouble(add1)+Double.parseDouble(add2);break; case 2: Result=Double.parseDouble(add1)-Double.parseDouble(add2);break; case 3: Result=Double.parseDouble(add1)*Double.parseDouble(add2);break; case 4: Result=Double.parseDouble(add1)/Double.parseDouble(add2);break; default:Option=0;} output+=" = "; tf.setText(output+Double.toString(Result)); OpClicked=false; Op=false; add1=""; add2=""; } } public static void main(String args[]) { keshihua2 calculator=new keshihua2(); calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
}
解决方案
由于double计算有误差,误差范围内都认为是正确的。
要得到精确的double值输出可以:
1.用String.format("%.2f", Result);格式化输出,其中.2表示保留小数点后2位(四舍五入后),可以根据你的输入确定该保留小数点后几位。不过这个方法在除法的时候大多时候只能得到近似值。
2.自已用INT实现double的运算。
时间: 2024-11-18 22:58:00