'end' 预期(在第 12 行关闭 'if')在 Mining Turtle 程序中

'end' expected (to close 'if' at line 12) in Mining Turtle program

我一直在研究一个程序,为我制作一个海龟矿。这是:

local depth = 0
local isJunk = true

function fuel()
    if turtle.getFuelLevel() < 20 then
        turtle.select(16)
        turtle.refuel(1)
    end
end
function up()
    fuel()
    if turtle.up() then
        return true
        depth = depth - 1
    else
        return false
    end
end
function down()
    fuel()
    if turtle.down() then
        return true
        depth = depth + 1
    else
        return false
    end
end
function checkWalls()
    for i = 1,4 do
        for j = 1,6 do
            turtle.select(i)
            if turtle.compare() then
                isJunk = true
            end
        end
        if isJunk == false then
            turtle.dig()
        end
        turtle.turnLeft()
    end
end
function digDown()
    for k = 1,6 do
        turtle.select(k)
        if turtle.compareDown() then
            if turtle.digDown() then
                return true
            else
                return false
            end
        end
    end
    turtle.select(1)
    turtle.digDown()
end
function digUp()
    for l = 1,6 do
        turtle.select(l)
        if turtle.compareUp() then
            if turtle.digUp() then
                return true
            else
                return false
            end
        end
    end
    turtle.select(1)
    turtle.digUp()
end

while true do
    term.clear()
    term.setCursorPos(1,1)
    print("-------Mining Operation Alpha-------")
    term.setCursorPos(1,2)
    term.write("Commence Mining Operation? (y/n): ")

    local input = read()

    if input  == "n" then
        term.setCursorPos(1,3)
        print("Cancelling Operation")
        sleep(1)
        exit()
    elseif input == "y" then
        term.setCursorPos(1,3)
        print("Commencing Alpha Mine")
        sleep(1)
     end

    digDn()
    down()
    digDn()
    down()
    turtle.select(7)
    turtle.placeUp()
    checkWalls()
    digDn()
    while down() do
        checkWalls()
        digDn()
    end
    up()
    turtle.select(15)
    turtle.placeDown()
    for m = 1,5 do
        up()
    end
    turtle.dig()
    fuel()
    turtle.forward()
    turtle.dig()
    fuel()
    turtle.forward()
    turtle.turnRight()
    turtle.dig()
    fuel()
    turtle.forward()
    turtle.turnLeft()
    digDn()
    while down() do
        digdn()
    end
    checkWalls()
    up()
    turtle.select(15)
    turtle.placeDown()
    checkWalls()
    while depth > 1 do
        digUp()
        up()
        checkWalls()
    end
    digUp()
    up()
    up()
    turtle.select(7)
    turtle.placeDown()
    fuel()
    turtle.forward()
    turtle.forward()
    turtle.turnRight()
    turtle.forward()
    turtle.turnLeft()
end

第 1-6 个插槽是我不想让它开采的物品,第 7 个是鹅卵石,第 15 个是火把,这样暴徒不会在竖井底部生成,第 16 个是煤炭。

每当我 运行 它时,我都会收到一条错误消息:

bios.lua:26: [string "mine.lua"]:14: 'end' expected (to close 'if' at line 12)

我看了看,有一个 'end' 的声明。但是,如果我注释掉 return 函数,它会起作用。

return 语句必须是块中的最后一个语句。

你可能想要

function up()
    fuel()
    if turtle.up() then
        depth = depth - 1
        return true
    else
        return false
    end
end