这段代码有什么问题? (groovy)MissingPropertyException

时间:2015-10-15 00:22:20

标签: groovy

我正在尝试编写一个小程序,读取三个点的X和Y坐标,然后输出三个中的哪一个更接近。但是我一直得到groovy.lang.MissingPropertyExeption错误。有人可以帮忙/解释出错了吗?

Point a = new Point()
print "enter first x co-ordinate: "
a.x = Double.parseDouble(System.console.readLine())
println "enter first y co-ordinate: "
a.y = Double.parseDouble(System.console.readLine())

Point b = new Point()
println "enter second x co-ordinate: "
b.x = Double.parseDouble(System.console.readLine())
println "enter second y co-ordinate: "
b.y = Double.parseDouble(System.console.readLine())

Point c = new Point()
println "enter third x co-ordinate: "
c.x = Double.parseDouble(System.console.readLine())
println "enter a third y co-ordinate: "
c.y = Double.parseDouble(System.console.readLine())

double distatob = Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) **2)
double distatoc = Math.sqrt((a.x - c.x) ** 2 + (a.y - c.y) **2)
double distbtoc = Math.sqrt((b.x - c.x) ** 2 + (b.y - c.y) **2)


if (distatob < distatoc && distatob < distbtoc) {
    println ("First and second points are closest")
} else if (distatoc < distatob && distatoc < distbtoc) {
    println ("First and third points are closest")
} else if (distbtoc < disatob && distbtoc < distatoc) {
    println ("Second and third co-ordinates are closest")
} else {
    println "error"
}

class Point {
    double x
    double y
}

1 个答案:

答案 0 :(得分:3)

错误是:groovy.lang.MissingPropertyException: No such property: console for class: java.lang.System

System.console不是属性。这是一种方法。因此,您需要调用它,如下所示:System.console().readLine()

也是拼写错误的变量名disatob而不是distatob,这是正确的版本:

Point a = new Point()
print "enter first x co-ordinate: "
a.x = Double.parseDouble(System.console().readLine())
println "enter first y co-ordinate: "
a.y = Double.parseDouble(System.console().readLine())

Point b = new Point()
println "enter second x co-ordinate: "
b.x = Double.parseDouble(System.console().readLine())
println "enter second y co-ordinate: "
b.y = Double.parseDouble(System.console().readLine())

Point c = new Point()
println "enter third x co-ordinate: "
c.x = Double.parseDouble(System.console().readLine())
println "enter a third y co-ordinate: "
c.y = Double.parseDouble(System.console().readLine())

double distatob = Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) **2)
double distatoc = Math.sqrt((a.x - c.x) ** 2 + (a.y - c.y) **2)
double distbtoc = Math.sqrt((b.x - c.x) ** 2 + (b.y - c.y) **2)


if (distatob < distatoc && distatob < distbtoc) {
    println ("First and second points are closest")
} else if (distatoc < distatob && distatoc < distbtoc) {
    println ("First and third points are closest")
} else if (distbtoc < distatob && distbtoc < distatoc) {
    println ("Second and third co-ordinates are closest")
} else {
    println "error"
}

class Point {
    double x
    double y
}