【发布时间】:2021-11-13 05:32:18
【问题描述】:
我有一个类,用户输入完美矩形左上角的 x 和 y 坐标,以及长度和宽度。 RectangleViewer 构造函数将矩形设置为空,然后将所有变量设置为 0。
这是我目前所拥有的:
public class RectangleViewer {
private Rectangle rectangle;
private Scanner input = new Scanner(System.in);
public RectangleViewer() {
this.rectangle = null;
this.rectangle = new Rectangle(new Point2D.Double(0.0, 0.0), 0.0, 0.0);
}
public void inputRectangleParameters() {
System.out.print("Please enter the x coordinate of the top left point of the rectangle: ");
double xCoordinate;
xCoordinate = input.nextDouble();
System.out.print("Please enter the y coordinate of the top left point of the rectangle: ");
double yCoordinate;
yCoordinate = input.nextDouble();
System.out.print("Please enter the length of the rectangle: ");
double inputLength;
inputLength = input.nextDouble();
System.out.print("Please enter the width of the rectangle: ");
double inputWidth;
inputWidth = input.nextDouble();
}
public void initializeRectangle() {
this.rectangle = new Rectangle(new Point2D.Double(inputRectangle());
}
}
这是 Rectangle 类本身的代码:
public class Rectangle {
private double width;
private double length;
private Point2D.Double topLeftPoint;
public Rectangle(Point2D.Double topLeftPoint, double width, double length) {
this.width = width;
this.length = length;
this.topLeftPoint = topLeftPoint;
}
我将如何设置它,以便将 inputRectangleParameters 中的值分配给将在 initializeRectangle 中创建的矩形?
【问题讨论】:
标签: java syntax geometry java.util.scanner rectangles