【发布时间】:2014-12-11 12:53:06
【问题描述】:
我正在编码,但这里出现错误:
Ammo = 450
Rocket = 20
Money = 35041
--Can I attend this mission?
function attendMission()
if Ammo >= 400 or Rocket >= 35 and Money >= 30000 then
return Money = Money - 30000
Msg("Attending Mission\n")
Msg("You now have "..Money.." dollars\n")
else
Msg("You do not have the resources to attend this mission\n")
end
end
print(attendMission())
--Ammo Amount
function whatIsAmmo()
if Ammo > 0 then
print("You have "..Ammo.." Ammo")
else
Msg("You have no Ammo")
end
end
print(whatIsAmmo())
--Rocket Amount
function whatIsRocket()
if Rocket > 0 then
Msg("You have "..Rocket.." rockets")
else
Msg("You have no rockets")
end
end
print(whatIsRocket())
--Money Amount
function whatIsMoney()
if Money > 100000 then
Msg("You have "..Money.." dollars, wow your rich!")
elseif Money > 0 then
Msg("You have "..Money.." dollars")
else
Msg("You have no money")
end
end
print(whatIsMoney())
它说在 = 附近有一个预期的结束。我一直在寻找一段时间,但我不知道如何解决它。我已经缩小了这个部分的错误范围:
function attendMission()
if Ammo >= 400 or Rocket >= 35 and Money >= 30000 then
return Money = Money - 30000
Msg("Attending Mission\n")
Msg("You now have "..Money.." dollars\n")
else
Msg("You do not have the resources to attend this mission\n")
end
end
【问题讨论】:
-
在 Lua 中(与 C 和其他一些语言不同),赋值是语句,而不是表达式。
-
好的,我做到了,但我现在在 'Msg' 附近得到“'end' 预期(在第 6 行关闭 'if')”
-
return也需要是块中的最后一条语句(至少在 lua 5.1 中)。所以这必须是块中的最后一行。虽然您可以使用do return end强制阻止使其工作。 -
你甚至不需要
return。Money = Money - 30000就足够了。 -
@EtanReisner:
return是否必须在末尾,在return之后立即声明是没有意义的。他们永远不会被处决。
标签: lua