2013-02-06 75 views

回答

4

Tables.他们就像一个文件柜在那里,只要你想,你可以保存尽可能多的价值和检索它们给一些“关键”的。在Lua中,密钥可以是任何类型,但最常见的密钥将是数字索引或字符串。

考虑:

local age = 30 -- your number values 
local name = 'Fred' -- your string value 

有一吨的不同的方式,我们可以结构在Lua:

local person = { age = 30, name = 'Fred') 
print(person.age, person.name) 

local person = { 'Fred', 30 } 
print(person[1], person[2]) 
print(unpack(person)) 

local person = { Fred = 30 } 
print(person.Fred) 

local person = { [30] = 'Fred' } 
print(person[30]) 

等等等等等等。

+1

'打印(人[0],人[1])' - 默认情况下以0表索引是众多的Lua程序员的一个梦想) –

+0

woops错了地方...... – Schmidty15

1

所以,如果我用..

coal = { name = "Coal", value = 80 } 

然后我就可以做到这一点?

userInput = read() 

    if userInput == coal.name then 
     fuelUse = coal.value   
    end 
相关问题