Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Input 从Lua中的用户获取输入_Input_Lua - Fatal编程技术网

Input 从Lua中的用户获取输入

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

如何从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.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