程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java代碼-我用java寫了一個簡易計算器的程序,但是有時候結果不正確

java代碼-我用java寫了一個簡易計算器的程序,但是有時候結果不正確

編輯:編程綜合問答
我用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的運算。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved