好的,我知道我之前提出了一个非常类似的问题,但我需要知道如何从用户那里取几个字并将它们存储到一个数组中并打印出来。我怀疑这需要一个for循环。我尝试了这段代码,但它不起作用:
System.in.withReader {
print 'input: '
def c1 = it.readLine()
def c2 = it.readLine()
def c3 = it.readLine()
def c4 = it.readLine()
def c5 = it.readLine()
def c6 = it.readLine()
def c7 = it.readLine()
def c8 = it.readLine()
def c9 = it.readLine()
def c10 = it.readLine()
country = [c1,c2,c3,c4,c5,c6,c7,c8,c9,c10]
assert country.size() == 10
for(i=0; i<10;i++)
{
println country[i]
}
}
有人有什么想法吗?提前谢谢。
答案 0 :(得分:1)
你走在正确的轨道上。试试这个:
System.in.withReader {
def country = []
for (i in 0..9)
country << it.readLine()
country.each() { println it }
}