【问题标题】:Lua Programming with ComputerCraft使用 ComputerCraft 进行 Lua 编程
【发布时间】:2014-03-10 01:42:18
【问题描述】:

我创建了一个小程序,询问您希望采矿龟开采的长度和高度。但是,当我在高级计算机中运行它时,它让我提示输入长度、宽度和高度,但随后出现错误。错误如下:

miner:39: 尝试索引? (一个零值)

这是我的代码:

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

【问题讨论】:

  • 任何帮助或评论表示赞赏
  • 您缺少 endfunction Length() 定义
  • @Schollii 我已经修改了我的代码,我不太明白你所说的while循环,如果可以的话,你会发布一些代码吗?
  • @hjpotter92 @ Schollii 我再次修改了我的代码,而不是 while 循环我使用了重复循环,但现在 CC 告诉我我正在索引一个 nil 值,请重新阅读我的问题 谢谢你
  • @Schollii 好的,我想通了!上次编辑后我的代码很好,我忘了把它放在海龟上哈哈!

标签: lua minecraft computercraft


【解决方案1】:

看起来你的任何函数都没有end,请先修复它。如果你正确缩进你的代码,你会看到这个。

您还拥有带有else 块的while count < length do。 AFAIK 这不是有效的语法(从未见过它,只是检查了在线参考手册和维基)。目前尚不清楚您是否指的是if count < length do,但如果真的指的是while,那么将else 替换为end 看起来也不正确。仔细看看那段代码。

【讨论】:

  • 我已经修改了我的代码,并用重复循环替换了所有的while循环,请看看你是否知道还有什么问题,我也改变了问题中出现的错误。感谢您的所有帮助。
猜你喜欢
  • 2020-12-30
  • 2015-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多