Lua - 从用户那里获取命令行输入?
-
06-07-2019 - |
题
在我的lua程序中,我想在继续操作之前停下来并要求用户确认。我不知道如何停止并等待用户输入,怎么办呢?
解决方案
查看 io
库,默认情况下将标准输入作为默认输入文件:
其他提示
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
尝试使用以下代码
<代码>
m = io.read() 如果m ==&quot; yes&quot;
(在此插入函数)
<代码>端代码>
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
这应该有效。
不隶属于 StackOverflow