用户输入以在字典中查找单词

时间:2019-02-11 04:09:50

标签: string dictionary input lua

我对编程还很陌生,想知道是否有人可以指出我正确的方向?

我正在尝试让用户输入并使用它来在字典中查找该单词,例如如果我输入“ jeff”,它将在字典中查找并告诉我“ 12”,因为cluthaTable["jeff"] = "12";

cluthaTable = {};
cluthaTable["jeff"] = "12";

local defaultField

local function textListener( event )

    if ( event.phase == "ended" or event.phase == "submitted" ) then
        -- Output resulting text from "defaultField"
        --print( event.target.text )

        -- Saves the input as a var for looking up name
        local test = event.target.text
            print(test)
    end
end


-- Create text field
defaultField = native.newTextField( 150, 150, 180, 30 )
defaultField:addEventListener( "userInput", textListener )

1 个答案:

答案 0 :(得分:0)

您尝试过吗?

local result = cluthaTable[event.target.text]

如果字典中存在这样的键,它将把该键的值保存到结果变量中。否则,结果变量将为nil。