You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现将RectangleViewer中用户输入的参数赋值给初始化的Rectangle对象

Fixing Your RectangleViewer Code to Use User Input

Let's break down the issues in your code and fix them step by step:

1. The Core Problems

  • Your inputRectangleParameters method reads user input but doesn't store those values anywhere, so initializeRectangle can't access them later.
  • The initializeRectangle method has syntax errors (mismatched parentheses, calling a non-existent inputRectangle() method) and no way to retrieve the user-provided values.

2. Solution Steps

We'll add member variables to store the user's input, update inputRectangleParameters to save those values, and fix initializeRectangle to use them correctly.

Modified RectangleViewer Code

import java.util.Scanner;
import java.awt.geom.Point2D;

public class RectangleViewer {
    private Rectangle rectangle;
    private Scanner input = new Scanner(System.in);
    // Add member variables to persist user input values
    private double topLeftX;
    private double topLeftY;
    private double inputLength;
    private double inputWidth;

    public RectangleViewer() {
        // No need to set rectangle to null first—direct assignment works perfectly
        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: ");
        topLeftX = input.nextDouble();
        
        System.out.print("Please enter the y coordinate of the top left point of the rectangle: ");
        topLeftY = input.nextDouble();
        
        System.out.print("Please enter the length of the rectangle: ");
        inputLength = input.nextDouble();
        
        // Optional: Validate length is a positive value (matches your "positive rectangle" requirement)
        while (inputLength <= 0) {
            System.out.println("Length must be a positive number. Please try again:");
            inputLength = input.nextDouble();
        }
        
        System.out.print("Please enter the width of the rectangle: ");
        inputWidth = input.nextDouble();
        
        // Optional: Validate width is a positive value
        while (inputWidth <= 0) {
            System.out.println("Width must be a positive number. Please try again:");
            inputWidth = input.nextDouble();
        }
    }

    public void initializeRectangle() {
        // Use the stored input values to create a valid Rectangle instance
        Point2D.Double topLeftPoint = new Point2D.Double(topLeftX, topLeftY);
        this.rectangle = new Rectangle(topLeftPoint, inputWidth, inputLength);
    }

    // Optional: Add a test method to verify functionality
    public static void main(String[] args) {
        RectangleViewer viewer = new RectangleViewer();
        viewer.inputRectangleParameters();
        viewer.initializeRectangle();
        // Uncomment below if you add getter methods to the Rectangle class
        /*
        System.out.println("Created rectangle details:");
        System.out.println("Top-left coordinate: (" + viewer.rectangle.getTopLeftPoint().x + ", " + viewer.rectangle.getTopLeftPoint().y + ")");
        System.out.println("Length: " + viewer.rectangle.getLength());
        System.out.println("Width: " + viewer.rectangle.getWidth());
        */
    }
}

Modified Rectangle Class (Add Getters for Verification)

If you want to confirm the rectangle was created correctly, add getter methods to your Rectangle class:

import java.awt.geom.Point2D;

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;
    }

    // Getter methods to access the rectangle's properties
    public double getWidth() {
        return width;
    }

    public double getLength() {
        return length;
    }

    public Point2D.Double getTopLeftPoint() {
        return topLeftPoint;
    }
}

3. Key Changes Explained

  • Added persistent member variables: topLeftX, topLeftY, inputLength, inputWidth store the user's input so initializeRectangle can access them later.
  • Simplified constructor: Setting rectangle to null before assignment is unnecessary—direct initialization is cleaner.
  • Fixed initializeRectangle: Creates a valid Point2D.Double from stored coordinates and passes all required values to the Rectangle constructor correctly.
  • Added input validation: Optional but practical to ensure the user enters positive values for length and width (aligning with your "positive rectangle" requirement).
  • Added a main method: To test the full flow of inputting parameters and initializing the rectangle.

内容的提问来源于stack exchange,提问作者ChanChan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 13:47:49