input - 如何使用 io.read 读取数字

标签 input lua

我是一个Lua初学者,不知道如何正确使用io.read等。 我正在做一些非常简单的事情,我想将 age = 18 语句替换为可以从用户那里获取输入的内容,要求他们说出自己的年龄。

而不是这个:

age = 18

if age >= 18 and age <=80 then
    print("You may enter!")
else
    print("You are not allowed in, sorry!")
end

我想要这个:

print("What's your age?")
io.read(ONLY ACCEPTS NUMBERS AND IS USED TO COMPARE WITH AGE REQUIREMENTS BELOW)

if age >= 18 and age <=80 then
    print("You may enter!")
else
    print("You are not allowed in, sorry!")
end

提前致谢。

最佳答案

您可以尝试使用 tonumber() 将变量转换为数字然后检查该变量的 bool 值。

print("What's your age?")
local age = tonumber(io.read())

if age and age >= 18 and age <= 80 then
    print("You may enter!")
else
    print("You are not allowed in, sorry!")
end

关于input - 如何使用 io.read 读取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41901892/

相关文章:

ruby - 生成街道 map 图像

lua - 描述Lua 5.2实现的文件?

html - 如何让一个按钮在右边,两个输入在左边来填充一行?

javascript - 设置输入字段的值

ionic-framework - Ionic3 输入不可选

javascript - 使用 indexOf 匹配用户输入

lua - 尝试索引字段(零值)

lua - 在 Lua 中,打印有哪些不同的形式?

lua - 如何将 ISO 8601 持续时间转换为 Lua 中的格式化字符串?

html - 如何更改type = time的html输入标签随附的小时钟图标的颜色