2015-10-15 30 views
-1

我想寫一個小程序,讀取三點的X和Y座標,然後輸出三者中哪一個更接近。但是我不斷收到groovy.lang.MissingPropertyExeption錯誤。有誰可以幫忙/解釋什麼是錯的?這段代碼有什麼問題? (groovy)MissingPropertyException

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 
} 

回答

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 
}