这段代码有什么问题? (groovy)MissingPropertyException [英] What is wrong with this code? (groovy) MissingPropertyException
问题描述
我试着编写一个小程序,读取三个点的X和Y坐标,然后输出三个中哪一个更接近。但是我不断收到groovy.lang.MissingPropertyExeption错误。
点a =新点()
打印输入第一个x坐标:
ax = Double.parseDouble(System.console.readLine())
println输入第一个y坐标:
ay = Double.parseDouble(System.console .readLine())
Point b = new Point()
println输入第二个x坐标:
bx = Double.parseDouble(System.console.readLine ))
println输入第二个y坐标:
by = Double.parseDouble(System.console.readLine())
Point c = new Point()
println输入第三个x坐标:
cx = Double.parseDouble(System.console.readLine())
println输入第三个y坐标:
cy = Double.parseDouble(System.console.readLine())
double distatob = Math.sqrt((ax - bx)** 2 +(ay - by)** 2)
double distatoc = Math.sqrt((ax - cx)** 2 +(ay - cy)** 2)
double distbtoc = Math.sqrt((bx - cx)** 2 +(by - cy )** 2)
if( distatob< distatoc&& distatob< distbtoc){
println(第一和第二点最接近)
} else if(distatoc< distatob& distatoc< distbtoc){
println(第一和第三点数最接近)
} else if(distbtoc< disatob&& distbtoc< distatoc){
println(第二个和第三个坐标最近)
} else {
printlnerror
}
class Point {
double x
double y
}
解决方案错误是:
groovy.lang.MissingPropertyException:没有这样的属性:类的控制台:java.lang.System
System.console
不是属性。这是一种方法。因此,您需要调用它,如下所示:System.console()。readLine()
。
还有拼写错误的变量名称
disatob
而不是distatob
,这里是正确的版本:点a =新点()
打印输入第一个x坐标:
ax = Double.parseDouble(System.console ().readLine())
println输入第一个y坐标:
ay = Double.parseDouble(System.console()。readLine())
Point b = new Point()
println输入第二个x坐标:
bx = Double.parseDouble(System.console()。readLine())
printlnenter second y co从属:
by = Double.parseDouble(System.console()。readLine())
Point c = new Point()
printlnenter third x co-坐标:
cx = Double.parseDouble(System.console()。readLine())
println输入第三个y坐标:
cy = Double.parseDouble(System。 console()。readLine())
double distatob = Math.sqrt((ax - bx)** 2 +(ay - by)** 2)
double distatoc = Math.sqrt((ax - cx)** 2 +(ay - cy)** 2)
double distbtoc = Math.sqrt (bx-cx)** 2 +(by-cy)** 2)
if(distatob< distatoc&& distatob< distbtoc){
println(第一和第二点最接近)
} else if(distatoc< distatob& distatoc< distbtoc){
println(第一和第三(distbtoc< distatob&& distbtoc< distatoc){
println(第二个和第三个坐标最接近)
} else {
printlnerror
}
class Point {
double x
double y
}
I am trying to write a small program that reads the X and Y coordinates of three points and then outputs which of the three are closer. However I keep getting groovy.lang.MissingPropertyExeption errors. Could anybody please help/explain what is wrong?
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 }
解决方案The error is:
groovy.lang.MissingPropertyException: No such property: console for class: java.lang.System
System.console
is not a property. It's a method. So you'll need to call it, like this:System.console().readLine()
.Also a misspelled variable name
disatob
instead ofdistatob
, here is the correct version: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 }
这篇关于这段代码有什么问题? (groovy)MissingPropertyException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!