Input 从Lua中的用户获取输入
如何从Lua中的用户处获取输入(如C中的Input 从Lua中的用户获取输入,input,lua,Input,Lua,如何从Lua中的用户处获取输入(如C中的scanf) 例如,程序询问用户的名字,然后他写下他的名字,然后程序将输出他的名字。最简单的输入可以使用io.read()检索。这将从标准输入读取一行(通常是键盘,但可以重定向,例如从文件) 您可以这样使用它: io.write('Hello, what is your name? ') local name = io.read() io.write('Nice to meet you, ', name, '!\n') io.read()只是io.inp
scanf
)例如,程序询问用户的名字,然后他写下他的名字,然后程序将输出他的名字。最简单的输入可以使用
io.read()
检索。这将从标准输入读取一行(通常是键盘,但可以重定向,例如从文件)
您可以这样使用它:
io.write('Hello, what is your name? ')
local name = io.read()
io.write('Nice to meet you, ', name, '!\n')
io.read()
只是io.input():read()
的快捷方式,类似地io.write()
也是io.output():write()的快捷方式
请注意,io.write()
不会像print()
那样自动终止您的行。使用io.read()注意,可以使用不同的参数自定义函数。这里有一些例子
s = io.read("*n") -- read a number
s = io.read("*l") -- read a line (default when no parameter is given)
s = io.read("*a") -- read the complete stdin
s = io.read(7) -- read 7 characters from stdin
x,y = io.read(7,12) -- read 7 and 12 characters from stdin and assign them to x and y
a,b = io.read("*n","*n") -- read two numbers and assign them to a and b
我建议使用io.stdin:read
而不是假设默认输入文件是stdin
。与io.stdout类似:write
。