读书人

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

发布时间: 2012-02-24 16:30:39 作者: rapoo

帮我看看这个问题,谢谢
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)的这个构造器,否则也会有误差。

读书人网 >J2SE开发

热点推荐