使用ComputerCraft进行Lua编程

我创建了一个小程序,询问您想要一个挖掘乌龟挖掘的长度、宽度和高度。当我在高级计算机上运行它时,它让我提示长度、宽度和高度,但是我会遇到一个错误。错误如下:

miner:39: attempt to index ? ( a nil value)

以下是我的代码:

term.clear()
term.setCursorPos(1,1)

write("Length:")
length = read()
print()
write("Confirm:")
ul = read()
print()

write("Width:")
width = read()
print()
write("Confirm:")
uw = read()
print()

write("Height:")
height = read()
print()
write("Confirm:")
uh = read()
print()

local totcount = ul + uw + uh
local subcount = 0

function Length()

repeat

    turtle.dig()
    turtle.forward()
    length = length - 1
    subcount = subcount + 1

until length == 0
length = ul

end

function Width()

repeat

    turtle.dig()
    turtle.forward()
    width = width - 1
    subcount = subcount + 1

until width == 0
width = uw
end

function Height()
turtle.digDown()
turtle.down()
height = height - 1
subcount = subcount + 1
end

function Turn()

turtle.turnRight()

end

repeat

Length()
Turn()
Width()
Turn()
Length()
Turn()
Width()
Turn()
Height()

until subcount == totcount
点赞
用户869951
用户869951

将下面翻译成中文并且保留原本的 markdown 格式

It doesn't look any of your functions have `end`, fix that first. If you properly indent your code you will see this.

You also have `while count < length do` with an `else` block. AFAIK this is not valid syntax (never seen it and just checked online ref manual and wiki). It is not clear whether you meant `if count < length do`, but if really meant `while` then replacing `else` by `end` doesn't look right either. Take a closer look at that section of code.

看起来你的函数都没有使用 end,先修正这个问题。如果你正确缩进你的代码,你就能看到这个错误。

你的代码中还有一个 while count < length do 循环,它带有一个 else 代码块。据我所知,这不是有效的语法(我从未见过这种用法,刚刚查询了一下在线的标准手册和维基百科)。不清楚你是否想使用 if count < length do,但如果你真想使用 while 循环,那么将 else 替换为 end 也不是恰当的做法。请仔细检查那个代码段。

2014-03-09 15:11:48