【问题标题】:User input values for a Rectangle矩形的用户输入值
【发布时间】: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


    【解决方案1】:

    inputRectangleParameters 方法从命令行读取值。如果您希望在initializeRectangle 方法中创建矩形,则需要将读取的值传递给它。

    这可以通过例如返回对象中的值。 由于 Rectangle 类包含值的相应属性,我建议在输入法中创建一个矩形对象并将其返回以进行初始化。

    public Rectangle 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();
    
        return new Rectangle(new Point2D.Double(xCoordinate,yCoordinate),inputWidth,inputLength);
    }
    
    public void initializeRectangle() {
        this.rectangle = inputRectangleParameters();
    }
    

    一个不太干净的版本是返回例如具有特定值位置的值数组。

    public double[] 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();
    
        return new double[]{xCoordinate, yCoordinate, inputWidth, inputLength};
    }
    
    public void initializeRectangle() {
        final double[] rectangleParameters = inputRectangleParameters();
        this.rectangle = new Rectangle(new Point2D.Double(rectangleParameters[0], rectangleParameters[1]), rectangleParameters[2], rectangleParameters[3]);
    }
    

    【讨论】:

    • 理论问题:如果 inputRectangleParameters 和 initializeRectangle 都是 void 方法,你会怎么做?
    • 通过存储值,例如作为类属性。对于带有数组的示例,通过添加 private double[] rectangleParameters;作为类属性并将其设置为: this.rectangleParameters = new double[]{xCoordinate, yCoordinate, inputWidth, inputLength};
    猜你喜欢
    • 2021-10-22
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多