我试图让xpos
和ypos
根据玩家进入的方向进行更改。该程序成功询问Where to?
但位置不变。该脚本也始终默认为最后一个条件(puts("You're drunk."))
。根据用户的输入更改变量
例如:
然而,我的输出是:
> n
You're drunk.
0
0
这是我当前的代码:
north = "n"
west = "w"
south = "s"
east = "e"
xpos = 0
ypos = 0
puts("WHERE TO?")
compass = gets
if compass == north
ypos = ypos + 1
elseif compass == west
xpos = xpos - 1
elseif compass == south
ypos = ypos - 1
elseif compass == east
xpos = xpos + 1
else
puts("You're drunk.") #if the player does not submit a proper direction
puts(xpos) #for debugging. prints zero when called showing the player has not moved
puts(ypos)
end
只是一个小边评论 - 在Ruby中,你并不总是需要大约函数参数的括号内。例如,这里的大多数'puts(...)'都可以成为'puts ...'。我注意到,这看起来像Ruby的方式来做事,尽管你的代码仍然是正确的。 –
你忘记了'chomp!'你的输入。 – squiguy
另一方面说明:至少就我所知,它是'elsif',而不是'elseif'。 –