Lua - 如何从用户获取命令行输入?

在我的 Lua 程序中,我想在执行操作之前停止并询问用户确认。我不确定如何停止并等待用户输入,该如何实现?

原文链接 https://stackoverflow.com/questions/1815163

点赞
stackoverflow用户148870
stackoverflow用户148870

请看默认情况下将标准输入作为默认输入文件的 io 库:

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

2009-11-29 10:19:27
stackoverflow用户107090
stackoverflow用户107090
local answer
repeat
   io.write("continue with this operation (y/n)? ")
   io.flush()
   answer=io.read()
until answer=="y" or answer=="n"

本地的答案 重复 io.write("继续此操作(y/n)? ") io.flush() answer=io.read() 直到答案=="y"或者答案=="n"

2009-11-29 10:30:33
stackoverflow用户1899359
stackoverflow用户1899359

我曾经在这样的代码上工作过。我会以这种方式键入代码,以使其有效:

io.write("继续进行此操作(y/n)?")
answer=io.read()
if answer=="y" then
   -- (在这里输入您想要执行的“y”响应)
elseif answer=="n" then
   -- (在这里输入您想要执行的“n”响应)
end
2012-12-12 23:11:30
stackoverflow用户2070226
stackoverflow用户2070226

我使用:

     print("是否继续(y/n)?")
re = io.read()
if re == "y" or "Y" then
    (在这里插入内容)
elseif re == "n" or "N" then
    print("好的...")
end
2013-02-13 22:40:50
stackoverflow用户2299722
stackoverflow用户2299722
m=io.read()
如果 m=="yes" 则
  (在此处插入函数)
end
2013-04-19 14:59:49
stackoverflow用户3123675
stackoverflow用户3123675
print("继续吗?(y/n)")
re = io.read()
if re == "y" or re == "Y" then
    #(在此处添加内容)
elseif re == "n" or re == "N" then
    print("好的......")
end

从我所做的lua代码 (不是很多)中,我要说,如果您使用string.sub,那么使用大小写字母是多余的。

print("继续吗?(y/n)")
local re = io.read()

--[[你能从局部变量中获得string.sub吗?
如果可以, 那么这会有效。
我对io不熟悉(游戏lua将GUI元素和按键替换为CLI)。]]

if re.sub == "y" then
    --做些事情
if re.sub == "n" then
    --做其他的事情
end

那应该行得通。

2013-12-20 17:54:38