表格的值返回了nil?

我在制作一个随机单词生成器时遇到了一个小问题。我想要从一个持有闭合字母(例如 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])

(这份代码仅仅是一个例子,我实际设置的更像这样)

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[math.random(#closedLetters)]..openLetters[math.random(#openLetters)])
点赞
用户2040431
用户2040431

你在表中仅仅拥有一堆带有nil值的键。因此,你的返回值是这些键。改为将它们设为字面量: closedLetters= {'a', 'b', .....}

2013-07-07 21:38:42
用户234175
用户234175

为了补充 Eyeball 的答案,你可以将closedLettersopenLetters变成实际的字符串。然后,你可以使用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) )
2013-07-07 21:50:39