在我的lua程序中,我想在执行操作之前停止并询问用户确认。我不确定如何停止并等待用户输入,应该如何实现?
在我的lua程序中,我想在执行操作之前停止并询问用户确认。我不确定如何停止并等待用户输入,应该如何实现?
local answer
repeat
io.write("continue with this operation (y/n)? ")
io.flush()
answer=io.read()
until answer=="y" or answer=="n"
我曾经处理过类似的代码。我会以一种可行的方式打出:
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
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
re == "y" or "Y"
应该改为 re == "y" or re == "Y"
。我们也可以检查 re:lower() == "y"
。请注意这一点可能很重要。 - PaulR尝试使用以下代码
m=io.read()
if m=="yes" then
(在此处插入函数)
end
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
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
那应该可以运行。
re.sub
will resolve to the function string.sub
& always be unequal to "y"
or "n"
. Besides, string matching is case sensitive. At best you can do re:match("[nN]")
and re:match("[yY]")
- Demur Rumed
io.read()
是否会强制执行自动的io.flush()
? - Egor Skriptunoff