在Lua中,循环如果语句。

我正在尝试循环一段代码,但遇到了一些问题。

我制作了一个程序,在程序中,您需要插入一个数字,然后PC会计算一些东西。

我的问题是,我不能循环if语句,以防止用户键入字母或类似内容。

下面是我需要循环的代码片段:

- 第一个数字
io.write("告诉我一个数字:")
a = io.read("*number")
- 键入字母
if a == nil
    然后
        io.write("\n",“对不起,这是无效的输入。", "\n")
        io.write("\n", "请告诉我一个数字:")
end

你能帮帮我吗?

我刚开始学习Lua编程,感到很困惑。

非常感谢。

点赞
用户88888888
用户88888888

你正在寻找一个... ...好了, 循环

local l = io.read("*line")
local a = tonumber(l)

while a == nil do
    print("sorry, invalid input")
    l = io.read("*line")
    a = tonumber(l)
end

(旁注:我不懂 Lua,我在谷歌上搜索了 2 分钟后找到了 tonumber() 函数。)

2013-09-08 10:22:11