-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