Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Input 如何使用io.read读取数字_Input_Lua - Fatal编程技术网

Input 如何使用io.read读取数字

Input 如何使用io.read读取数字,input,lua,Input,Lua,我是Lua初学者,不知道如何正确使用io。阅读等等。 我正在做一件非常简单的事情,我想将age=18语句替换为可以从用户那里获得输入的内容,让他们说出自己的年龄 与此相反: age = 18 if age >= 18 and age <=80 then print("You may enter!") else print("You are not allowed in, sorry!") end 年龄=18岁 如果年龄>=18且年龄=18且年龄,则可以尝试使用将变

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

与此相反:

age = 18

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

如果年龄>=18且年龄=18且年龄,则可以尝试使用将变量转换为数字,然后检查该变量的布尔值

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
print(“您的年龄是多少?”)
本地年龄=tonumber(io.read())
如果年龄和年龄>=18且年龄,Lua解释器将为您打开。您可以使用
io.read
读取数字,并且如所述,您可以使用
n
格式指示您要读取整数或浮点

local age = io.stdin:read 'n'
if age then
  -- age is a number
else
  -- age is nil
end
local age = io.stdin:read 'n'
if age then
  -- age is a number
else
  -- age is nil
end