catking李 2013-11-11 06:29 采纳率: 100%
浏览 2842
已采纳

我用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);
  }

}

  • 写回答

1条回答 默认 最新

  • ll_0520 2013-11-11 09:50
    关注

    由于double计算有误差,误差范围内都认为是正确的。
    要得到精确的double值输出可以:
    1.用String.format("%.2f", Result);格式化输出,其中.2表示保留小数点后2位(四舍五入后),可以根据你的输入确定该保留小数点后几位。不过这个方法在除法的时候大多时候只能得到近似值。
    2.自已用INT实现double的运算。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀