Random 表值返回零?

Random 表值返回零?,random,lua,Random,Lua,我正在做一个随机单词生成器,我遇到了一个小问题。我试图从一个包含closedLetters(c、d、f等)的表中打印值,但它不起作用。它返回零。帮忙 local closedLetters={b, c, d, f, g, h, j, k, l, m, n, p, q, r, s, t, v, w, x, z} local openLetters={a, e, i, o, u, y} print(closedLetters[2]) (那段代码只是一个例子,我设置的实际上更像这样) 那张桌子上只有

我正在做一个随机单词生成器,我遇到了一个小问题。我试图从一个包含closedLetters(c、d、f等)的表中打印值,但它不起作用。它返回零。帮忙

local closedLetters={b, c, d, f, g, h, j, k, l, m, n, p, q, r, s, t, v, w, x, z}
local openLetters={a, e, i, o, u, y}
print(closedLetters[2])
(那段代码只是一个例子,我设置的实际上更像这样)


那张桌子上只有一串钥匙。键具有nil值,因此返回值。 改为使用文字:
closedLetters={'a','b',…}

为了补充眼球的答案,您可以只制作
closedLetters
openlets
实际字符串。然后可以使用
string.sub
访问它们:

local closedLetters = "bcdfghjklmnpqrstvwxz"
local openLetters   = "aeiouy"
local letter1, letter2 = math.random(#closedLetters), math.random(#openLetters)
print(closedLetters:sub(letter1, letter1) .. openLetters:sub(letter2, letter2) )

这太棒了,而且很方便。然而,由于lua中的“一切都是表”。我认为我们的解决方案基本上是相同的:)@眼球实际上弦并不是隐藏的桌子。表是可变的,字符串不是。酷。谢谢你的启发:)
local closedLetters = "bcdfghjklmnpqrstvwxz"
local openLetters   = "aeiouy"
local letter1, letter2 = math.random(#closedLetters), math.random(#openLetters)
print(closedLetters:sub(letter1, letter1) .. openLetters:sub(letter2, letter2) )