Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么groovy在字典中看不到一些值?_Groovy - Fatal编程技术网

为什么groovy在字典中看不到一些值?

为什么groovy在字典中看不到一些值?,groovy,Groovy,我开始学习groovy,因为我喜欢通过实践学习,所以我编写了一个小应用程序: def height = 1; def commands; def floors; def finished = false def up = { height < 5 ? height++ : println("Can't go higher!") } def down = { height > -1 ? height-- : println("Can't go lower!") } def help

我开始学习groovy,因为我喜欢通过实践学习,所以我编写了一个小应用程序:

def height = 1; def commands; def floors; def finished = false

def up = { height < 5 ? height++ : println("Can't go higher!") }
def down = { height > -1 ? height-- : println("Can't go lower!") }
def help = { 
    print "Commands are " 
    commands.each{key, val -> print "'$key' "}
    println() 
}
def printFloors = {
    println "Floors are "
    floors.each{key,val -> println "'$key' -> '$val'"}
    println()
}
def exit = { finished = true }
def prompt = { print floors["$height"] }

commands = [ 'u': up, 
             'up': up,
             'd': down,
             'down': down,
             'help': help,
             '': help,
             'exit': exit,
             'pf': printFloors]

floors = [ "-1": "Basement : " ,
           "0": "Ground : " ,
           "5": "Penthouse : " ]
(1..4).each{floors += ["${it}" : "Floor ${it} : " ] }

bR = new BufferedReader(new InputStreamReader(System.in))

while(!finished){
    prompt()
    def cmd = bR.readLine()
    def code = commands[cmd]
    if(code != null){
        code()
    }
}
def高度=1;def命令;def地板;def finished=false
def up={height<5?height++:println(“不能再高了!”)}
def down={height>-1?height--:println(“不能再低了!”)}
def help={
打印“命令是”
命令。每个{key,val->打印“$key”}
println()
}
def打印楼层={
println“地板是”
楼层。每个{key,val->println“'$key'->'$val'}
println()
}
def exit={finished=true}
def提示符={打印楼层[“$height”]}
命令=['u':向上,
“向上”:向上,
d:向下,
“向下”:向下,
“帮助”:帮助,
'':救命,
“退出”:退出,
“pf”:打印楼层]
楼层=[“-1”:“地下室:”,
“0”:“地面:”,
“5”:“阁楼:”]
(1..4).每个{floors+=[“${it}”:“Floor${it}:”]}
bR=新的BufferedReader(新的InputStreamReader(System.in))
当(!完成){
提示符()
def cmd=bR.readLine()
def代码=命令[cmd]
如果(代码!=null){
代码()
}
}
除了打印您所在的楼层(提示功能),其他一切都可以正常工作。如果您在地下室、一楼或阁楼,它会打印,但不会拾取“楼层i:”,而是打印空值:/ 当我输入“pf”打印我的字典时,值就在那里。。。 有什么想法吗?
谢谢

您正在地图中添加
GString
实例作为键,然后使用
String
实例搜索它们

两者不一样(尽管外观相同——请参阅“gstring不是字符串”一节)

尝试更改:

(1..4).each{floors += ["${it}" : "Floor ${it} : " ] }


一个等价的行是:
(1..4)。每个{floors[它作为字符串]=“Floor${it}:”}
@epidemian我更喜欢:
floors@tim_yates是的,我也更喜欢这样。并且可能用
('1'..'4')
来代替
('1'..'4')
。@epidemian我想了想
('1'..'4')
,但决定在他们到达第十层时可能会引起问题;-)
(1..4).each{floors += [ ("${it}".toString()): "Floor ${it} : " ] }