在 Lua 中获取用户的输入 [英] Getting input from the user in Lua

查看:40
本文介绍了在 Lua 中获取用户的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Lua 中获取用户的输入(如 C 中的 scanf)?
例如,程序询问用户他的名字,然后他写下他的名字,然后程序就会输出他的名字.

How can I get an input from user in Lua (like scanf in C)?
For example, the program ask user his name, then he writes his name, then the program will output his name.

推荐答案

使用 io.read() 注意函数可以自定义不同的参数.以下是一些示例.

Use io.read() Beware that the function can be customised with different parameters. Here are some examples.

 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

这篇关于在 Lua 中获取用户的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆