首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

帮小弟我看看这个有关问题,多谢

2012-02-24 
帮我看看这个问题,谢谢importjavax.swing.JOptionPanepublicclassyuan{publicstaticvoidmain(Stringargs[

帮我看看这个问题,谢谢
import   javax.swing.JOptionPane;
public   class   yuan{
  public   static   void   main(String   args[])
  {
      String   Number;

     
      double   r,d,c,s;
     
       
      Number=JOptionPane.showInputDialog( "Enter   the   integer ");
     
   

      r=Double.parseDouble(Number);
     
   
      d=2*r;
     

      c=2*3.14*r;

      s=3.14*r*r;
     

      JOptionPane.showMessageDialog(
          null, "The   d   is   "+d, "Results ",JOptionPane.PLAIN_MESSAGE);
     

      JOptionPane.showMessageDialog(
          null, "The   c   is   "+c, "Results ",JOptionPane.PLAIN_MESSAGE);
       

      JOptionPane.showMessageDialog(
          null, "The   s   is   "+s, "Results ",JOptionPane.PLAIN_MESSAGE);
         

 

      System.exit(0);
    }
}  

输入4以后得到的结果是:25.12,这个没问题;
但是输入5以后得到的结果却是:31.00000000000002.
为什么呢?

[解决办法]
先看看这里吧..
http://community.csdn.net/Expert/topic/5731/5731238.xml?temp=.5735437
[解决办法]
小数计算要用BigDecimal类型,否则会有误差。而且在new对象时一定要用new BigDecimal(String value)的这个构造器,否则也会有误差。

热点排行