如何從Lua中的用戶獲取輸入(如C中的scanf
)?
例如,程序詢問用戶他的名字,然後他寫下他的名字,然後程序會輸出他的名字。從Lua中的用戶獲取輸入
5
A
回答
15
使用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
4
最簡單的輸入可以使用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()
。 See the API for read()
here。
請注意,io.write()
不會像print()
那樣自動終止您的行。
相關問題
- 1. Lua - 從用戶那裏獲取命令行輸入?
- 2. 從語音中獲取用戶輸入?
- 3. Lua和用戶輸入
- 4. 從HTML表單獲取用戶輸入
- 5. 從後臺獲取用戶輸入
- 6. 從用戶處獲取輸入
- 7. 從JtextArea獲取用戶輸入
- 8. 從用戶獲取輸入號碼
- 9. 從JavaFX獲取用戶輸入
- 10. 從JTextArea獲取用戶輸入
- 11. Java:從JTextField獲取用戶輸入
- 12. 獲取文件mime輸入Corona/Lua?
- 13. 獲取用戶輸入
- 14. 獲取用戶輸入
- 15. 獲取用戶輸入
- 16. Java獲取用戶輸入
- 17. 獲取用戶輸入
- 18. C++ Lua從Lua表中獲取值
- 19. 使用輸入()在八度中獲取STDIN的用戶輸入?
- 20. 從用戶讀取輸入
- 21. 如何從團隊中獲取團隊用戶從輸入中獲取
- 22. 如何使用Python中的輸入框從用戶處獲取輸入?
- 23. 如何從ocaml中的用戶輸入中獲取char列表
- 24. 獲取每個項目陣列中的從用戶輸入中
- 25. 在c#中獲取用戶輸入
- 26. 在awk中獲取用戶輸入
- 27. 獲取Lua中os.execute的輸出
- 28. 如何從用戶輸入的ocaml中獲取值的列表
- 29. 從草稿中的用戶獲取輸入的文本
- 30. 從C#中的用戶處獲取輸入的方法
我會建議使用'io.stdin:read'而不是假定默認輸入文件是'stdin'。與'io.stdout:write'類似。 –