“Unexpected symbol near '<'” 的意思是“在'<'附近发现了意料之外的符号”。

我正在尝试在 Lua 中编写一些代码,只有当你年龄为12岁时,它才会打印“欢迎!”。 然而,每当我运行这段代码时,我都会收到一个错误消息,上面写着:

在 '<' 附近发现意外的符号。

错误消息说这在第 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
点赞
用户4761240
用户4761240

你有不必要的 ==

将代码更改为:

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

当你检查一个变量是否大于小于另一个变量时,不需要使用 ==

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


这也可能会有所帮助:

作为你的第一个 Lua 代码,也许需要注意,如果你想检查一个变量是否大于等于(或小于等于),你需要写成 if (5 >= 5) thenif (3 <= 3) then

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

例如:if (7 == 7) then

2015-07-07 12:12:58