gpt4 book ai didi

lua - 对于循环不完全正确的 Lua

转载 作者:行者123 更新时间:2023-12-01 21:48:33 26 4
gpt4 key购买 nike

for amountofspinstodo = 1, 100 do
local pick = math.random( 1, #box.CONTENTS )
local rarity = INV:CalculateItemRarity()
local ITEMPICK = INV:GetDataFromName(box.CONTENTS[pick])
local RARITYS_OF_ITEM_PICK = ITEMPICK.RARITYS
if has_value(RARITYS_OF_ITEM_PICK, rarity)then
tbl.spintable[amountofspinstodo] = { NAME = box.CONTENTS[pick], RARITY = rarity }
print(amountofspinstodo)
else
amountofspinstodo = amountofspinstodo - 1
print(amountofspinstodo)
end
end

我做了这个 for 循环来检查一个项目是否具有一定的稀有性,如果确实如此,则允许它成为一个项目,但如果它不具有稀有性,那么它应该让 for 循环执行此操作一次,直到每个项目都被挑选。然而,当我运行 for 循环时,它会这样做,我不知道为什么。

Here is the console output

一些数字是重复的,例如 48 和 48 应该是 48 和 49。

如有任何帮助,我们将不胜感激!

-谢谢 D12

最佳答案

amountofspinstodo 无法从您的 for 循环内部更改,并且会持续到循环的下一次运行。

引用资料:

Third, you should never change the value of the control variable: The effect of such changes is unpredictable. If you want to break a for loop before its normal termination, use break. - Programming in Lua: 4.3.4 – Numeric for

这是一个您可以用来查看的简单示例:

for i = 1, 10 do
print(i)
i = 10
end

相反,您应该使用 while 循环:

local amountofspinstodo = 1
while(amountofspinstodo < 100) do
local pick = math.random( 1, #box.CONTENTS )
local rarity = INV:CalculateItemRarity()
local ITEMPICK = INV:GetDataFromName(box.CONTENTS[pick])
local RARITYS_OF_ITEM_PICK = ITEMPICK.RARITYS
if has_value(RARITYS_OF_ITEM_PICK, rarity)then
tbl.spintable[amountofspinstodo] = { NAME = box.CONTENTS[pick], RARITY = rarity }
print(amountofspinstodo)
amountofspinstodo = amountofspinstodo + 1
else
print(amountofspinstodo)
end
end

关于lua - 对于循环不完全正确的 Lua,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59847370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com