Lua - get command line input from user?

隐身守侯 提交于 2019-11-30 07:09:35

问题


In my lua program, i want to stop and ask user for confirmation before proceeding with an operation. I'm not sure how to stop and wait for user input, how can it be done?


回答1:


Take a look at the io library, which by default has standard-input as the default input file:

http://www.lua.org/pil/21.1.html




回答2:


local answer
repeat
   io.write("continue with this operation (y/n)? ")
   io.flush()
   answer=io.read()
until answer=="y" or answer=="n"



回答3:


I've worked with code like this. I will type this in a way it will work:

io.write("continue with this operation (y/n)?")
answer=io.read()
if answer=="y" then
   --(put what you want it to do if you say y here)
elseif answer=="n" then
   --(put what you want to happen if you say n)
end



回答4:


I use:

     print("Continue (y/n)?")
re = io.read()
if re == "y" or "Y" then
    (Insert stuff here)
elseif re == "n" or "N" then
    print("Ok...")
end



回答5:


try to use folowing code

m=io.read() if m=="yes" then (insert functions here) end




回答6:


print("Continue (y/n)?")
re = io.read()
if re == "y" or "Y" then
    (Insert stuff here)
elseif re == "n" or "N" then
    print("Ok...")
end

From the bit of lua that I've done (not a lot), I'm going to say that using both uppercase and lowercase letters is redundant if you use string.sub.

print("Continue? (y/n)")
local re = io.read()

--[[Can you get string.sub from a local var? 
If so, this works. I'm unfamiliar with io(game 
lua uses GUI elements and keypresses in place of the CLI.]]

if re.sub == "y" then
    --do stuff
if re.sub == "n" then
    --do other stuff
end

That should work.



来源:https://stackoverflow.com/questions/1815163/lua-get-command-line-input-from-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!