Lua - 从用户那里获取命令行输入?

发布于 2024-08-12 08:17:18 字数 59 浏览 3 评论 0原文

在我的 lua 程序中,我想在继续操作之前停止并要求用户确认。我不知道如何停止并等待用户输入,该怎么办?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

顾挽 2024-08-19 08:17:18
local answer
repeat
   io.write("continue with this operation (y/n)? ")
   io.flush()
   answer=io.read()
until answer=="y" or answer=="n"
local answer
repeat
   io.write("continue with this operation (y/n)? ")
   io.flush()
   answer=io.read()
until answer=="y" or answer=="n"
就此别过 2024-08-19 08:17:18

看一下 io 库,它默认将标准输入作为默认输入文件:

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

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

手长情犹 2024-08-19 08:17:18

我曾经使用过这样的代码。我将以一种有效的方式输入:

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

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
反差帅 2024-08-19 08:17:18

我用:

     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

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
一身骄傲 2024-08-19 08:17:18

尝试使用以下代码


m=io.read()

如果 m==“是”那么
(在此处插入函数)
结束

try to use folowing code


m=io.read()

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

标点 2024-08-19 08:17:18
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

那应该有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文