Input 如何在Lua中的同一行上进行用户输入?

Input 如何在Lua中的同一行上进行用户输入?,input,io,lua,Input,Io,Lua,这是我的密码: while true do opr = io.read() txt = io.read() if opr == "print" then print(txt) else print("wat") end end 我想做的是在您键入print的地方打印,然后按如下所示: print text 它将打印文本,但在键入打印后,如果不按enter键,我似乎无法在同一行上打印。我总是不得不这样写: print text

这是我的密码:

while true do
    opr = io.read() txt = io.read()
    if opr == "print" then
        print(txt)
    else
        print("wat")
    end
end
我想做的是在您键入
print
的地方打印,然后按如下所示:

print text
它将打印
文本
,但在键入
打印
后,如果不按enter键,我似乎无法在同一行上打印。我总是不得不这样写:

print
text

如果有人知道我如何解决这个问题,请回答。

好吧,那是因为io.read()实际上读取整行内容。 你要做的是读一行:

command = op.read()
然后分析字符串。 对于您想要做的事情,最好是迭代字符串,寻找空格来分隔每个单词并将其保存到表中。那你就可以随心所欲了。 您还可以在迭代时动态解释命令:

Read in the first word;
if it is "print" then read in the rest of the line and print it;
if it is "foo" read in the next 3 words as aprameters and call bar();
等等


现在,我将把实现留给您。如果需要帮助,请留下评论。

这是因为io.read()实际上读取整行内容。 你要做的是读一行:

command = op.read()
然后分析字符串。 对于您想要做的事情,最好是迭代字符串,寻找空格来分隔每个单词并将其保存到表中。那你就可以随心所欲了。 您还可以在迭代时动态解释命令:

Read in the first word;
if it is "print" then read in the rest of the line and print it;
if it is "foo" read in the next 3 words as aprameters and call bar();
等等


现在,我将把实现留给您。如果需要帮助,请留下注释。

在没有参数的情况下调用时,
io.read()。您可以通过模式匹配来阅读这行文字:

input = io.read()
opr, txt = input:match("(%S+)%s+(%S+)")

上述代码假设只有一个单词表示
opr
,一个单词表示
txt
。如果可能有零个或多个
txt
,请尝试以下操作:

while true do
    local input = io.read()
    local i, j = input:find("%S+")
    local opr = input:sub(i, j)
    local others = input:sub(j + 1)
    local t = {}
    for s in others:gmatch("%S+") do
        table.insert(t, s)
    end
    if opr == "print" then
        print(table.unpack(t))
    else
        print("wat")
    end
end

在没有参数的情况下调用时,
io.read()
读取整行。您可以通过模式匹配来阅读这行文字:

input = io.read()
opr, txt = input:match("(%S+)%s+(%S+)")

上述代码假设只有一个单词表示
opr
,一个单词表示
txt
。如果可能有零个或多个
txt
,请尝试以下操作:

while true do
    local input = io.read()
    local i, j = input:find("%S+")
    local opr = input:sub(i, j)
    local others = input:sub(j + 1)
    local t = {}
    for s in others:gmatch("%S+") do
        table.insert(t, s)
    end
    if opr == "print" then
        print(table.unpack(t))
    else
        print("wat")
    end
end


对不起,我第一次理解错了你的问题。假设这两个词总是在同一行,或者你想要一个既适用于同一行中的词又适用于不同行中的词的东西,可以吗。。。我想要它,当你输入“打印资料”时,它会打印“资料”。我想为一个项目开个“编程语言”的玩笑。我想要打印让它知道它正在打印一些东西,然后在打印之后再打印单词。您可以键入“print which”并按enter键,而不是键入“print”enter“which”enter。可以假定最后每行都有一个命令,并且每个命令都写在一行中吗?为什么不简单地读取整个字符串(opr+txt),然后将其拆分为一个命令及其参数(如果有)?这个问题可能是相关的:对不起,我第一次错误地理解了你的问题。假设这两个词总是在同一行,或者你想要一个既适用于同一行中的词又适用于不同行中的词的东西,可以吗。。。我想要它,当你输入“打印资料”时,它会打印“资料”。我想为一个项目开个“编程语言”的玩笑。我想要打印让它知道它正在打印一些东西,然后在打印之后再打印单词。您可以键入“print which”并按enter键,而不是键入“print”enter“which”enter。可以假定最后每行都有一个命令,并且每个命令都写在一行中吗?为什么不简单地读取整个字符串(opr+txt),然后将其拆分为一个命令及其参数(如果有)?此问题可能与此相关:某些命令可能没有参数或有多个参数。我只遇到一个错误。。。它只打印第一个单词(例如,打印这个,打印那个,打印这个),如果你能帮忙的话,那就太好了!在原始的if之后,我也无法执行任何其他操作。“尝试调用字段‘unpack’(一个nil值)”错误消息。@Prince-替换
表格。unpack
unpack
某些命令可能有零个参数或多个参数。我只遇到一个错误。。。它只打印第一个单词(例如,打印这个,打印那个,打印这个),如果你能帮忙的话,那就太好了!在原始if之后,我也无法执行任何其他操作。“尝试调用字段“unpack”(一个空值)”错误消息。@Prince-替换
表。使用
unpack解包
解包谢谢您的帮助。谢谢您的帮助。