0

我试图让一个变量具有数字值和字符串值。

我正在用 Lua 编码,但我不知道该怎么做。可能吗?

4

2 回答 2

4

表。它们就像一个文件柜,您可以在其中存储任意数量的值,并在给定某种“密钥”的情况下检索它们。在 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])

等等等等。

于 2013-02-06T04:46:30.533 回答
1

So if i use..

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

I can then do this?

    userInput = read()

    if userInput == coal.name then
        fuelUse = coal.value        
    end
于 2013-02-06T16:33:08.060 回答