用户在字典中查找单词

我刚开始学习编程,想知道是否有人能指点我一下。

我想使用用户输入的单词,在字典中查找这个单词。例如,如果我输入“jeff”,它会在字典中查找并告诉我“12”,因为 cluthaTable["jeff"] = "12";

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

local defaultField

local function textListener( event )

    if ( event.phase == "ended" or event.phase == "submitted" ) then
        -- 从“defaultField”中输出结果文本
        --print( event.target.text )

        -- 将输入保存为用于查找名称的变量
        local test = event.target.text
            print(test)
    end
end

-- 创建文本字段
defaultField = native.newTextField( 150, 150, 180, 30 )
defaultField:addEventListener( "userInput", textListener )
点赞
用户7632578
用户7632578

你试过这个了吗?

local result = cluthaTable[event.target.text]

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

2019-02-11 07:23:18