在Java程序练习时发现的一个数值上的问题

public class Rectangle
{
	double width;
	double height;
	String color;
	Rectangle()
	{
		width=1;
		height=1;
		color="white";
	}
	Rectangle(double width,double height)
	{
		this.width=width;
		this.height=height;
	}
	double rectangle_Width()
	{
		return width;
	}
	double rectangle_Height()
	{
		return height;
	}
	String rectangle_color()
	{
		return color;
	}
	void trim(String color)
	{
		this.color=color;
	}
	double getArea()
	{
		return width*height;
	}
	double getPerimeter()
	{
		return 2*(width+height);
	}
	public static void main(String[] args)
	{
		Rectangle r1 = new Rectangle(4,40);
		Rectangle r2 = new Rectangle(3.5,35.9);
		r1.trim("red");
		r2.trim("red");
		System.out.println("Now your shape1 color is "+r1.color);
		System.out.println("Now your shape2 color is "+r2.color);
		System.out.println("Now your shape1 area is "+r1.getArea());
		System.out.println("Now your shape2 area is "+r2.getArea());
		System.out.println("Now your shape1 perimeter is "+r1.getPerimeter());
		System.out.println("Now your shape2 perimeter is "+r2.getPerimeter());
	}
}
程序运行后r2的面积显示为125.64999999999,实际结果应该是125.65,记录一下这个问题,也很想从热心的朋友处得到答案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值