我是一个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/