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,记录一下这个问题,也很想从热心的朋友处得到答案。