有没有办法在for循环中为lua中的变量添加数值?

时间:2016-02-21 01:11:24

标签: lua

我制作了这个节目:

a = math.random(0, 9) - 0 -- In this stage, if the result equals zero, that means it's a match
b = math.random(0, 9) - 1
c = math.random(0, 9) - 2
d = math.random(0, 9) - 3
e = math.random(0, 9) - 4
f = math.random(0, 9) - 5
g = math.random(0, 9) - 6
h = math.random(0, 9) - 7
i = math.random(0, 9) - 8
j = math.random(0, 9) - 9

print("Enter the number of trials you want to simulate.") -- This is where I decide how many trials I want to do

var = io.read()

a_ = 0 -- This is where I hope to keep the number of "a" matches, number of "b" matches, etc. The frequency
b_ = 0
c_ = 0
d_ = 0
e_ = 0
f_ = 0
g_ = 0
h_ = 0
i_ = 0
j_ = 0

for k = 1, var do -- One loop is a trial

    print("Trail #"..k)

    if a == 0 then
        print("a = match")
    elseif a ~= 0 then
        print("a = not a match")
    end

    if b == 0 then
        print("b = match")
    elseif b ~= 0 then
        print("b = not a match")
    end

    if c == 0 then
        print("c = match")
    elseif c ~= 0 then
        print("c = not a match")
    end

    if d == 0 then
        print("d = match")
    elseif d ~= 0 then
        print("d = not a match")
    end

    if e == 0 then
        print("e = match")
    elseif e ~= 0 then
        print("e = not a match")
    end

    if f == 0 then
        print("f = match")
    elseif f ~= 0 then
        print("f = not a match")
    end

    if g == 0 then
        print("g = match")
    elseif g ~= 0 then
        print("g = not a match")
    end

    if h == 0 then
        print("h = match")
    elseif h ~= 0 then
        print("h = not a match")
    end

    if i == 0 then
        print("i = match")
    elseif i ~= 0 then
        print("i = not a match")
    end

    if j == 0 then
        print("j = match")
    elseif j ~= 0 then
        print("j = not a match")
    end

end

while true do --This is just to keep the window open after the program is done so that I can observe the data, you can ignore this
end

正如你所看到的,我试图在每次返回结果为零时向a_,b_和c_添加一个,但它不起作用,有没有办法做到这一点? 我想这样做的原因是我正在参加的AP统计课程,这将使它更容易做到。我现在只是做a_,b_,c_,一旦我解决了这个问题,我会做所有这些。谢谢你的阅读!

1 个答案:

答案 0 :(得分:0)

假设您希望模拟运行'var'次,请尝试以下操作:

math.randomseed(os.time())

local matchStorage = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
local randomNums = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

local function runSimulation(numTrial)
    print("\nRunning trial #: " .. numTrial)

    for index, value in pairs(randomNums) do
        local variable = math.random(0, 9)
        randomNums[index] = variable
    end

    for index, value in pairs(randomNums) do
        if randomNums[index] == 0 then
            matchStorage[index] = matchStorage[index] + 1
            print("index " .. index .. " is a match.")
        else
            print("index " .. index .. " is not a match.")
        end
    end
end

do
    print("Enter the number of trials")
    numTrials = io.read()

    for index = 1, numTrials do
        runSimulation(index)
    end

    print("\nRESULTS:\n")

    for index, value in pairs(matchStorage) do
        print("index " .. index .. " frequency: " .. value)
    end
end

以下'randomNum'值之一包含0的次数将存储在其对应的'matchStorage'索引中。

相关问题