Groovy 意外标记:在第行时:
我是Groovy新手,没有Java方面的实际操作经验。谁能用下面的代码回答我天真的问题Groovy 意外标记:在第行时:,groovy,Groovy,我是Groovy新手,没有Java方面的实际操作经验。谁能用下面的代码回答我天真的问题 class StudentMap { def student_map=new HashMap<String,String>() def answer=System.console().readLine 'Do you want to add a student Y/N\n' while (answer=='Y') { get_student() answer=Syst
class StudentMap {
def student_map=new HashMap<String,String>()
def answer=System.console().readLine 'Do you want to add a student Y/N\n'
while (answer=='Y') {
get_student()
answer=System.console().readLine 'Do you want to continue Y/N\n'
}
def add_student(String name,String age)
{
student_map << ["${name}":"${age}"]
}
def get_student()
{
def name=System.console().readLine 'What is your name\n'
def age=System.console().readLine 'What is your age\n'
add_student(name,age)
}
print student_map
}
def Student_Map = new StudentMap()
class StudentMap{
def student_map=new HashMap()
def answer=System.console().readLine“是否要添加学生Y/N\N”
while(答案=='Y'){
得到你的学生()
answer=System.console().readLine“是否继续?”
}
def add_学生(字符串名称、字符串年龄)
{
学生地图问题在于你的while
循环和打印学生地图
语句,它们超出了任何方法
因此,如果要执行while并打印,请将它们放在main
方法中:
class StudentMap {
...
//something like
static void main(String[] args) {
while (answer=='Y') {
get_student()
answer=System.console().readLine 'Do you want to continueY/N\n'
}
print student_map
}
}
Groovy是一种脚本语言,所以您不需要将代码放入类中。另一个选项是从脚本中删除类声明
Groovy reference:谢谢victor,它现在编译得很好,但仍然面临一些问题。请您浏览我的最新帖子,告诉我我做错了什么。提前谢谢
class StudentMap {
void main(String[] args) {
answer=System.console().readLine 'Do you want to add a student Y/N\n'
def student_map=new HashMap<String,String>()
while (answer=='Y') {
get_student()
answer=System.console().readLine 'Do you want to continueY/N\n'
}
}
def add_student(String name,String age) {
student_map << ["${name}":"${age}"]
}
def get_student()
{
def name=System.console().readLine 'What is your name\n'
def age=System.console().readLine 'What is your age\n'
add_student(name,age)
}
} def Student_Map = new StudentMap()
class StudentMap {
...
//something like
static void main(String[] args) {
while (answer=='Y') {
get_student()
answer=System.console().readLine 'Do you want to continueY/N\n'
}
print student_map
}
}