Lua意外的符号在'<'附近

kat*_*sha 2 lua garrys-mod

我正在尝试在Lua中编写一些东西,你必须在12岁之前将它打印出来"Welcome!".但是,每当我运行此代码时,我都会收到一条错误消息

'<'附近的意外符号.

错误消息表明这是第3行.如果可能,是否有人还可以指出此代码中的其他潜在错误?我的代码如下:

io.write ("Enter your age:")
    age = io.read()
if age == <12 then
    print ("O noes, you are too young!")
elseif age == >12 then
    print ("O noes, you are too old!")
else 
    print ("Welcome, son!")
end
Run Code Online (Sandbox Code Playgroud)

Nol*_* M. 5

你有不必要==的.

将代码更改为:

io.write ("Enter your age:")
age = io.read()
if age < 12 then
    print ("O noes, you are too young!")
elseif age > 12 then
    print ("O noes, you are too old!")
else 
    print ("Welcome, son!")
end
Run Code Online (Sandbox Code Playgroud)

当您检查变量是否大于小于另一个变量时,您不需要==.

例: if (7 < 10) then if (9 > 3) then


这可能也有帮助:

由于这是您的第一个Lua代码,因此如果您正在检查变量是否大于或等于(或小于或等于),您可能需要将其编写为if (5 >= 5) thenif (3 <= 3) then.

只有==在检查它是否等于另一个变量时才需要.

例: if (7 == 7) then