【问题标题】:Issues with io.read() in wxLUA (Does not wait for user input, Always returns nil)wxLUA 中的 io.read() 问题(不等待用户输入,始终返回 nil)
【发布时间】:2015-03-04 21:48:22
【问题描述】:

我刚刚进入 Lua,但我遇到了用户输入问题。无论我做什么,io.read() 总是返回 nil。

这是我尝试过的,没有任何效果。

name = io.read()
print(name)

name = io.read(7)
print(name)

print(io.read:())

所有这些都只是打印 nil。 我认为正在发生的事情是 name 在用户有机会实际输入值之前设置为 io.read()

这个:

 while not name do name = io.read()
 print name

只是导致程序因某种短路而崩溃,可能是因为循环完成并且应用程序在几分之一秒内刷新。

我使用的是 Windows 的 wxLua 环境,在对话框版本中这些程序运行良好,但你显然不能这样做。

【问题讨论】:

    标签: lua user-input wxlua


    【解决方案1】:

    我不确定你为什么要尝试io.read(7)(因为这将读取 7 个字节),但是下面的脚本适用于我的 wxlua(虽然我使用我自己编译的 wxlua 库,你可以得到 here ):

    require('wx')
    io.write("Enter something: ")
    local value = io.read()
    io.write(value, "\n")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      • 2020-06-27
      相关资源
      最近更新 更多