【问题标题】:What is wrong with this lua coding?这个lua编码有什么问题?
【发布时间】: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 强制阻止使其工作。
  • 你甚至不需要returnMoney = Money - 30000 就足够了。
  • @EtanReisner:return 是否必须在末尾,在 return 之后立即声明是没有意义的。他们永远不会被处决。

标签: lua


【解决方案1】:

我刚刚发现了问题所在。显然是移回一些词来创造:

Ammo = 450
Rocket = 20
Money = 35041
--Can I attend this mission?
function attendMission()
if Ammo >= 400 or Rocket >= 35 and Money >= 30000 then
    Msg("Attending Mission\nYou now have "..Money.." dollars\n")
    Money = Money - 30000
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())

这样就完美了。但是,谢谢你们帮助我!尤其是你的评论 Josh,他教会了我一些新东西;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    • 2012-08-07
    • 2014-08-29
    • 2013-06-05
    • 2014-06-04
    • 2011-07-07
    • 1970-01-01
    相关资源
    最近更新 更多