欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Lua程序设计第4版第8章课后练习答案

程序员文章站 2024-03-17 23:01:16
...

8.1
没有switch,elseif比使用多个if性能更高

8.2
while(true)
for 1,math.huge
repeat
goto

8.3
你想怎样都好

8.4

function room1()
    local move = io.read()
    if move == "south" then
        room3()
    elseif move == "east" then
        room2()
    else
        print("invalid move")
        room1()
    end
end
function room2()
    local move = io.read()
    if move == "south" then
        room4()
    elseif move == "west" then
        room1()
    else
        print("invalid move")
        room2()
    end
end
function room3()
    local move = io.read()
    if move == "north" then
        room1()
    elseif move == "east" then
        room4()
    else
        print("invalid move")
        room3()
    end
end
function room4()
    print("congratulations , you won!")
end

room1()

8.5
假如goto语句能跳出函数,直接跳转到某个变量的作用域,却绕过了变量的声明与赋值,会发生无法预料的错误,本来局部的变成使用全局的人也可能

8.6
原理同上

相关标签: LUA