如何修复此错误:“尝试索引?(数字值)”

时间:2014-02-15 15:52:18

标签: lua lua-table

local questions={
    EN={
        Q1={"",""},
        Q2={"",""},
        Q3={"",""},
        Q4={"",""},
        Q5={"",""},
        Q6={"",""},
        Q7={"",""},
        Q8={"",""},
        Q9={"",""},
        Q10={"",""}
    },
    BR={
        Q1={"",""},
        Q2={"",""},
        Q3={"",""},
        Q4={"",""},
        Q5={"",""},
        Q6={"",""},
        Q7={"",""},
        Q8={"",""},
        Q9={"",""},
        Q10={"",""}
    },
    TR={
        Q1={"",""},
        Q2={"",""},
        Q3={"",""},
        Q4={"",""},
        Q5={"",""},
        Q6={"",""},
        Q7={"",""},
        Q8={"",""},
        Q9={"",""},
        Q10={"",""}
    }
}
local current_question = questions.EN.Q..math.random(1,10)[1]

我想让current_question变量从questions表中返回一个随机问题,但它会给我一个错误; “尝试索引?(数字值)”

1 个答案:

答案 0 :(得分:2)

您需要告诉解释器Q..math.random(1,10)是这样的一个字符串:

local current_question = questions.EN["Q"..math.random(1,10)][1]