-1
我做了这个程序:有没有办法在for循环中为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_,一旦我解决了这个问题,我会全部做完。谢谢阅读!
请提供您的代码的简化版本,您的代码很长,而且没有组织。 – warspyking
我不知道如何简化它,对不起。我对Lua不是很擅长,这是我能做的最好的。有关简化的任何提示? – ZackaryCW
如果这有助于我的目标是 – ZackaryCW