8.1
大概是因为lua不支持swtich结构,并且每个代码块结束都有一个end,如果多次使用 i f e l s e ^{else}_{if} ifelse这种写法的话,end的个数太多,影响代码可读性。
8.2
while true do
expression
end
for i=1,math.huge do
expression
end
repeat
expression
until nil
::s1:: do
expression
goto s1
end
8.3
不知道怎么回答,lua里的repeat-until和c里的do-while是差不多的意思
8.4
#!/usr/bin/lua
function room1()
local move=io.read()
if move=="south" then
return room3()
elseif move=="east" then
return room2()
else
print("Invalid room!")
return room1()
end
end
function room2()
local move=io.read()
if move=="west" then
return room1()
elseif move=="south" then
return room4()
else
print("Invalid room!")
return room2()
end
end
function room3()
local move=io.read()
if move=="north" then
return room1()
elseif move=="east" then
return room4()
else
print("Invalid room!")
return room3()
end
end
function room4()
print("Congratulations, you won!")
end
room1()
8.5
待完善
8.6
待完善