【问题标题】:Is there a way to make 3 cases on this code?有没有办法在此代码上制作 3 个案例?
【发布时间】:2020-11-25 00:48:10
【问题描述】:

我制作了一个不和谐机器人,并在不和谐服务器上寻求有关此代码的一些帮助。他们建议在这里问。

这是原始代码

    if(msg.content.toLowerCase() === "yandev, buy something"){
    var Patreon = Math.random();
        if (Patreon > 0.5)
       {
             msg.channel.send('image link');
             msg.channel.send('Just bought this with my patreon money.');

       }
        else
        {
             msg.channel.send('image link');
             msg.channel.send('Just bought 3 of these with my patreon money.')
        }

})

这是我在不和谐人的帮助下编写的代码

    if(msg.content.toLowerCase() === "yandev, buy something"){
    var Patreon = Math.random();
        if (Patreon > 0.5)
       {
             msg.channel.send('image link');
             msg.channel.send('Just bought this with my patreon money.');

       }
        else
        {
             msg.channel.send('image link');
             msg.channel.send('Just bought 3 of these with my patreon money.')
        }
        else if
        {
             msg.channel.send('image link')
             msg.channel.send('Just bought this with my patreon money.')
        }
})  

这两个都给出相同的错误:

                else if
                ^^^^

SyntaxError: Unexpected token 'else'
                else
                ^^^^

SyntaxError: Unexpected token 'else'

我尝试自己修复它,但找不到解决方法。 所以现在我在这里。谢谢。

【问题讨论】:

  • else if 需要一个条件,else 应该在else if 之后定义,因为它只会在所有其他条件都失败时运行。
  • "如果 A 为真,做 B,否则做 C,否则如果 D 为真,做 E"。这对你有意义吗?
  • 查看MDN Docs 了解if/else if/else 控制语句。

标签: javascript discord.js


【解决方案1】:

您需要切换elseelse if。订单必须始终为if -> else if -> else。您还缺少else if 中的一个条件。

这是有道理的,因为最后一个 else 将涵盖您尚未明确检查的所有条件。

【讨论】:

    【解决方案2】:

    底部的右括号是从哪里来的? 从第一个块中提供的代码来看应该是。

       if(msg.content.toLowerCase() === "yandev, buy something"){
        var Patreon = Math.random();
            if (Patreon > 0.5)
           {
                 msg.channel.send('image link');
                 msg.channel.send('Just bought this with my patreon money.');
    
           }
            else
            {
                 msg.channel.send('image link');
                 msg.channel.send('Just bought 3 of these with my patreon money.')
            }
    
    }
    

    第二个代码块实际上没有意义,因为你的 else if 没有条件。

    尝试删除第一个块最后一行的')',看看它是否有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-30
      • 2022-11-16
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-14
      相关资源
      最近更新 更多