我在循环一段代码时遇到了一些问题。
我制作了一个程序,你必须插入一个数字,然后PC计算一些东西。
我的问题是我无法循环阻止用户输入字母或类似内容的if语句。
这是我需要循环的代码片段:
-- first number
io.write("Tell me a number: ")
a = io.read("*number")
-- typing a letter
if a == nil
then
io.write("\n", "Sorry, this is an invalid imput.", "\n")
io.write("\n", "Please tell me a number: ")
end
你能帮我吗?
我刚刚开始在Lua编程,我很困惑。
非常感谢你。
答案 0 :(得分:2)
您正在寻找......嗯,循环:
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()
功能。)