lua - lua中的多个for
问题描述
我需要为变量事物制作超过 1 个“for”,我认为这不是一个好方法,如何在 1 个循环中添加多个 for?它的工作,但我需要一个正确的方法来做到这一点
for i=1, #mush01Plants, 1 do
if GetDistance... then
...
end
end
for i=1, #mush02Plants, 1 do
if GetDistance... < 1 then
...
end
end
for i=1, #mush03Plants, 1 do
if GetDistance... < 1 then
...
end
end
解决方案
您可以创建一个函数来“模板化”代码:
local function GetDistanceForPlants(plants) -- plants would be `mush01Plants`-like tables.
for i=1, #plants, 1 do
if GetDistance... then
...
end
end
end
GetDistanceForPlants(mush01Plants)
GetDistanceForPlants(mush02Plants)
GetDistanceForPlants(mush03Plants)
如果您需要使用每个表中的某些内容,此函数将很有用,如果不需要,只需#mush01Plants + #mush02Plants + #mush03Plants
在一个循环中求和即可。
推荐阅读
- python - Python透明水印不透明
- excel - VBA:关于在以下过程后返回目标(工作表更改事件)单元格或至少返回目标工作表
- matlab - Octave:warning: 'optimoptions' 函数属于 Octave Forge 的 optim 包,但尚未实现
- nginx - 30 秒后出现 502 错误网关
- java - 如何编写使用方法显示和用户可以输入输入的java
- java - 更新 JAVA 中由特定字符分隔的文件的特定位置
- c++ - 在函数内部使用数组。对函数内部数组所做的更改对其他函数不可见
- loss-function - 具有 CTC 损失的 CNN
- azure - 工作站的 Azure 备份失败。没有数据传输到云端
- javascript - 我可以更改什么以使此代码适用于所有部分元素