【发布时间】:2018-06-20 18:15:14
【问题描述】:
我想创建一个表,其中特定名称作为键,特定功能作为值。 键名代表用户输入的命令,如果存在该名称的键,则程序应执行存储在该键值中的代码。
例如,我们在键值中创建一个包含键和函数的表:
local t = {
["exit"] = quitGame,
...,
...
}
我们还有一个函数,例如:
function quitGame()
print("bye bye")
os.exit()
end
所以现在我们这样做:
userInput = io.read()
for i,v in pairs(t) do
if userInput == i then
--now here, how do I actually run the code that is stored in that key value (v)?
end
end
我希望你明白我想要做什么。
【问题讨论】: