Java 矩形 class area/perimeter 输出 0
Java Rectangle class area/perimeter outputting 0
当我得到 运行 我的 Rectangle.java 时,我可以在矩形中得到输入的长度和宽度,但是当我尝试用 [=31= 计算 area/perimeter ] 结果是零
我尝试添加和删除 setter,将 getter/setter 方法放入 getArea 和 getPerimeter 中,但似乎没有任何效果
//Code provided by the teacher as a template
Rectangle temp = new Rectangle();
temp.print();
System.out.println();
temp.setLength(2.5);
temp.setWidth(3.0);
//Consider how your rectangle will change after setting the length and width to specific values.
temp.print();
System.out.println();
Rectangle r = new Rectangle(3.5,2);
r.print();
//My Class
public class Rectangle
{
private static double length;
private static double width;
private static double perimeter;
private static double area;
public Rectangle(double length, double width)
{
setLength(length);
setWidth(width);
}
public Rectangle()
{
}
public static double getLength()
{
return length;
}
public static void setLength(double length)
{
Rectangle.length = length;
}
public static double getWidth()
{
return width;
}
public static void setWidth(double width)
{
Rectangle.width = width;
}
public static double getPerimeter(double length, double width)
{
return 2*width+2*length;
}
public static double getArea(double length, double width)
{
area= getLength()*getWidth();
return length*width;
}
public static String print()
{
String Rectangle = new String();
System.out.println("This rectangle has a length of "+length+" and a width of "+width);
System.out.println("The area of the rectangle is: "+ area);
System.out.println("The perimeter of the rectangle is: "+ perimeter);
return Rectangle;
}
}
没有错误消息。
输出:
这个矩形的长度为 0.0,宽度为 0.0
矩形的面积为:0.0
长方形的周长为:0.0
这个矩形长2.5,宽3.0
矩形的面积为:0.0
长方形的周长为:0.0
这个矩形长3.5,宽2.0
矩形的面积为:0.0
长方形的周长为:0.0
您需要更改打印方法并调用周长和面积方法,以便这些变量获得所需的值
public static String print()
{
getPerimeter(length,width);
getArea(length,width);
String Rectangle = new String();
System.out.println("This rectangle has a length of "+length+" and a width of "+width);
System.out.println("The area of the rectangle is: "+ area);
System.out.println("The perimeter of the rectangle is: "+ perimeter);
return Rectangle;
}
我建议不要使用静态关键字,因为值不会绑定到对象
当我得到 运行 我的 Rectangle.java 时,我可以在矩形中得到输入的长度和宽度,但是当我尝试用 [=31= 计算 area/perimeter ] 结果是零
我尝试添加和删除 setter,将 getter/setter 方法放入 getArea 和 getPerimeter 中,但似乎没有任何效果
//Code provided by the teacher as a template
Rectangle temp = new Rectangle();
temp.print();
System.out.println();
temp.setLength(2.5);
temp.setWidth(3.0);
//Consider how your rectangle will change after setting the length and width to specific values.
temp.print();
System.out.println();
Rectangle r = new Rectangle(3.5,2);
r.print();
//My Class
public class Rectangle
{
private static double length;
private static double width;
private static double perimeter;
private static double area;
public Rectangle(double length, double width)
{
setLength(length);
setWidth(width);
}
public Rectangle()
{
}
public static double getLength()
{
return length;
}
public static void setLength(double length)
{
Rectangle.length = length;
}
public static double getWidth()
{
return width;
}
public static void setWidth(double width)
{
Rectangle.width = width;
}
public static double getPerimeter(double length, double width)
{
return 2*width+2*length;
}
public static double getArea(double length, double width)
{
area= getLength()*getWidth();
return length*width;
}
public static String print()
{
String Rectangle = new String();
System.out.println("This rectangle has a length of "+length+" and a width of "+width);
System.out.println("The area of the rectangle is: "+ area);
System.out.println("The perimeter of the rectangle is: "+ perimeter);
return Rectangle;
}
}
没有错误消息。
输出:
这个矩形的长度为 0.0,宽度为 0.0 矩形的面积为:0.0 长方形的周长为:0.0
这个矩形长2.5,宽3.0 矩形的面积为:0.0 长方形的周长为:0.0
这个矩形长3.5,宽2.0 矩形的面积为:0.0 长方形的周长为:0.0
您需要更改打印方法并调用周长和面积方法,以便这些变量获得所需的值
public static String print()
{
getPerimeter(length,width);
getArea(length,width);
String Rectangle = new String();
System.out.println("This rectangle has a length of "+length+" and a width of "+width);
System.out.println("The area of the rectangle is: "+ area);
System.out.println("The perimeter of the rectangle is: "+ perimeter);
return Rectangle;
}
我建议不要使用静态关键字,因为值不会绑定到对象