【发布时间】:2021-01-13 04:24:47
【问题描述】:
我的作业中有这个问题
练习:For循环
填写每个 for_loop#() 函数以使用 for 循环创建所需的数组
require "testwell"
function for_loop1(len)
local out = {}
-- Put your code between here ****************
-- and here **********************************
return out
end
is(for_loop1(4), {1,2,3,4}, 'For loop array creation len = 4')
is(for_loop1(9), {1,2,3,4,5,6,7,8,9}, 'For loop array creation len = 9')
我不确定如何传递我通常使用的数组长度
n={1,2,3...}
没有指定大小。 我试过这个
unction for_loop1(len)
local out = {}
-- Put your code between here ****************
for i= 1,4,4 do
out ={1,2,3,4} end
for i= 1,9,9 do
out={1,2,3,4,5,6,7,8,9}
end
-- and here **********************************
return out
end
但它不起作用
【问题讨论】:
-
Lua 没有设置数组长度的语句。只需开始将数据插入空数组即可。
-
听起来你需要了解numerical for loop。使用它来分配表中的每个索引。
-
@EgorSkriptunoff 我试过了:out = {1,2,3,4} 并得到了第一个输出,但我不知道如何从同一个块中获取另一个数组大小为 9。如您所见,我需要编写接收任何大小的方法并相应地输出数组。
-
@luther 我确实通过了这个链接,但我需要的是一种将数组的大小作为参数并从列表中相应地输出数组的方法。我不确定我是否以正确的方式解释了我的问题,我刚刚开始学习 Lua。