【问题标题】:if, else, else if and end Luaif, else, else if 和结束 Lua
【发布时间】:2012-05-11 15:06:38
【问题描述】:

知道为什么这在 Lua 中是错误的吗?

       if Pieza == 1 then
            if Rotacion == 1 then
                Piezas = Cuadrado1
            else if Rotacion == 2 then
                Piezas = Cuadrado2
            else if Rotacion == 3 then --this is Line 273
                Piezas = Cuadrado3
            else if Rotacion == 4 then
                Piezas = Cuadrado4
            else
                io.write("Me Envio una rotacion que no existe? \n");
            end 
--this end closes the if inside the first if the one using to compare Rotacion
        else if Pieza == 2 then
            if Rotacion == 1 then
                Piezas = I1
            else if Rotacion == 2 then
                Piezas = I2
            else if Rotacion == 3 then
                Piezas = I3
            else if Rotacion == 4 then
                Piezas = I4
            else
                io.write("Me Envio una rotacion que no existe? \n");
            end
--this end closes the if inside the first if the one using to compare Rotacion
        else  --this else is in case Pieza != 1 || 2
            io.write("Me Envio una pieza que no existe? \n");
        end --this close the whole if (the one comparing "Pieza")

我得到的错误是“else”附近的预期“结束”(在第 273 行关闭“if”)

另外,在每个 if 之后添加 end (这不是应该做的,但无论如何都尝试过)不起作用......

如果你需要我翻译成英文,我很乐意做评论和一切,但我认为这个问题没有必要。

【问题讨论】:

  • 这是个好问题。刚开始用 Lua 编程时,我遇到了同样的问题!
  • 哈哈是的,实际上我已经习惯了 c++ 和 c#,所以如果对我来说似乎是正确的 XD

标签: lua if-statement


【解决方案1】:

它是elseif,而不是else if(注意空格)。错误是因为解释器期望每个else 块都有一个end

更多信息请参见the manual

【讨论】:

  • 期望答案可以替代许多 if/else,例如 switch 语句 :-)
  • Lua 没有 switch 语句。
猜你喜欢
  • 2015-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多