在我的lua程序中,我想在继续操作之前停下来并要求用户确认。我不知道如何停止并等待用户输入,怎么办呢?
答案 0 :(得分:30)
local answer
repeat
io.write("continue with this operation (y/n)? ")
io.flush()
answer=io.read()
until answer=="y" or answer=="n"
答案 1 :(得分:13)
查看io
库,默认情况下将标准输入作为默认输入文件:
答案 2 :(得分:7)
我使用过这样的代码。我将以一种可行的方式输入:
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
答案 3 :(得分:1)
我用:
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
答案 4 :(得分:1)
尝试使用以下代码
m=io.read()
if m=="yes" then
(insert functions here)
end
答案 5 :(得分:-1)
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
从我已经完成的lua(不是很多),我要说如果你使用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
这应该有效。