问题描述
<$ c我想用for循环创建多个变量(名字与最后一个字符相同) $ c> for i = 1,10,1 do
marker + i =do things
end
几乎我想要得到的是:marker0,marker1,marker2等等。我猜是有问题的标记+我
我得到一个错误。谢谢。
你可能不想这样做。更简单的做法是创建一个表格并在表格中创建这些变量作为键。
t = {}
for i = 1,10,1 do
t [marker.. i] =做事情
结束
(请注意, ..
是contuanation,而不是 +
in lua。而且你需要引用一个字符串,而不是直接使用它。)
但是,如果你真的希望这些是全局变量而不是键在一些其他的表中,你可以通常(取决于环境)做以下操作:
pre $ code>对于i = 1,10,1 do
_G [marker.. i] =do things
end
I want to use a for loop to create multiple varibles (with names that are the same except for the last character) in lua
for i= 1, 10, 1 do
marker+i = "do things"
end
pretty much I what I want to get is: marker0, marker1, marker2 and so on. and I guess there is something wrong with marker+i
I get an error. Thank you.
You probably don't want to do this actually. Much simpler would be to create a table and create those variables as keys in the table.
t={}
for i=1, 10, 1 do
t["marker"..i] = "do things"
end
(Note that ..
is contatenation and not +
in lua. Note also that you need to quote a string and not use it literally.)
But if you really want those to be global variables and not keys in some other table you can generally (depending on environment) do the following
for i=1, 10, 1 do
_G["marker"..i] = "do things"
end
这篇关于在lua中使用for循环创建多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!