如何实现将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
inputRectangleParametersmethod reads user input but doesn't store those values anywhere, soinitializeRectanglecan't access them later. - The
initializeRectanglemethod has syntax errors (mismatched parentheses, calling a non-existentinputRectangle()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,inputWidthstore the user's input soinitializeRectanglecan access them later. - Simplified constructor: Setting
rectangletonullbefore assignment is unnecessary—direct initialization is cleaner. - Fixed
initializeRectangle: Creates a validPoint2D.Doublefrom 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
相关产品推荐
相关产品推荐

