试图自学Lua;我已经经历过类似的问题,但我仍然不明白该怎么做。使我感到困惑的主要是表与数组。对于下面的代码,我只想对照已填充到数组中的值检查给定值。但是,出了点问题。谢谢你的时间
valueToCheckFor = 35
sampleArray = {}
for i=30, 49, 1 do
sampleArray[i] = i + 1
print(i)
end
for k = 0, #sampleArray, 1 do
if valueToCheckFor == k then
print(valueToCheckFor .. " is in the array.")
else
print(valueToCheckFor .. " is not in the array.")
end
end
最佳答案
这是编写对Lua数组友好的代码:
valueToCheckFor = 35
sampleArray = {}
for i=30, 49 do
-- add to end of array
sampleArray[#sampleArray+1] = i + 1
print(i+1)
end
-- check each value in array
for k = 1, #sampleArray do
if valueToCheckFor == sampleArray[k] then
print(valueToCheckFor .. " is in the array.")
else
print(valueToCheckFor .. " is not in the array.")
end
end