我需要編寫一個程序來讀取兩個點的X和Y座標,然後輸出矩形的面積和周長,其中兩個點都是對角。然而我得到這個錯誤信息MissingPropertyException錯誤(groovy)
groovy.lang.MissingPropertyException:No這樣的屬性:x對於類: 矩形。
有沒有人請幫忙解釋一下這裏出了什麼問題?
// First point
Point point1 = new Point()
print "enter first x co-ordinate: "
point1.x = Double.parseDouble(System.console().readLine())
print "enter first y co-ordinate: "
point1.y = Double.parseDouble(System.console().readLine())
// Second point
Point point2 = new Point()
print "enter second x co-ordinate: "
point2.x = Double.parseDouble(System.console().readLine())
print "enter second y co-ordinate: "
point2.y = Double.parseDouble(System.console().readLine())
// Create Rectangle
Rectangle myRectangle = new Rectangle()
myRectangle.upLeft = point1
myRectangle.downRight = point2
// Calculate Perimeter
double width = myRectangle.downRight.x - myRectangle.upLeft.x
double height = myRectangle.upLeft.y - myRectangle.downRight.y
double perimeter = 2 * (width + height)
// Calculate Area
double area = width x height
println "Perimeter is " + perimeter
println "Area is " + area
class Point {
double x
double y
}
class Rectangle {
Point upLeft
Point downRight
}
這是整個代碼? –