在for循环中将数据保存到数组中 - Lua

在我的 Lua 程序中,我想要用 for 循环检查红石输入的侧面。 最后几行用于将数组的结果打印出来作为调试工具。输出是侧面名称后跟数字 0(期望是 true/false)。稍后程序将使用这些数据。

谢谢。

function array()
  a = {}
  for i=1, 6 do
    a[i] = {}

    for j=1, 2 do
      a[i][j] = 0
    end
  end
  a[1][1]= "front"
  a[2][1]= "back"
  a[3][1]= "left"
  a[4][1]= "right"
  a[5][1]= "top"
  a[6][1]= "bottom"
end

array()

for i=1, 6 do
  input=redstone.getInput(a[i][1])
  if input=="true" then
    a[2][2]="true"

  elseif input=="false" then
    a[3][2]="false"
  end
end
for i=1, 6 do
  print(a[i][1])
  print(a[i][2])
end
点赞
用户1009479
用户1009479

redstone.getInput()手册中得知,它返回的是布尔值,而不是字符串,因此这行代码:

if input == "true" then

应该改为

if input == true then

同样适用于elseif部分。

2014-03-23 03:07:55