【问题标题】:Multiple operatores in one if statement [duplicate]一个if语句中有多个运算符[重复]
【发布时间】:2020-12-31 12:14:00
【问题描述】:

我做了如下声明:

if (command !== "define" || (command !== "add") || (command !== "remove")) throw "ERROR 1";

它在 try if 语句中。 我想要它,以便如果命令不是“定义”、“添加”或“删除”,它会引发错误。 但是,即使我做了类似的事情

/money 定义,它仍然会抛出错误。 分开,它看起来像这样:

if (command !== "define") throw "ERROR 1";
if (command !== "add") throw "ERROR 2";
if (command !== "remove") throw "ERROR 3";

完整代码:

mp.events.addCommand('dinheiro', (player, _, command, amount, targetPlayer) => {
    var isSuccess = true;
    try {
        if (command !== "definir" || (command !== "adicionar") || (command !== "retirar")) throw "ERORR 1";
    }
    catch (err) {
        isSuccess = false;
    }
    if (isSuccess) {
      //code
    }

我删除了不必要的部分,但我总是抛出 ERROR 1,所以问题很可能出在 if 语句中。

【问题讨论】:

  • 如果它等于其中一个字符串,那么它也等于其他两个。因此,该语句的计算结果始终为true
  • 怎么样?我希望它逐个字符串检查它是否相等。如果我不能一次完成?
  • 你可以,但你想要&&而不是||
  • 就是这样。谢谢。我总是这样读:如果不是这个,不是这个,不是这个,那么
  • if( !["define", "add", "remove"].includes(command) ) throw "ERORR";

标签: javascript node.js if-statement


【解决方案1】:

if (command !== "define" && (command !== "add") && (command !== "remove")) throw "ERROR 1";

or 运算符 (||) 将在其中一个操作数的计算结果为 true 时立即返回 true,例如如果命令是“添加”,则第一个子句为真,因为命令不是“定义”。

【讨论】:

    猜你喜欢
    • 2011-01-19
    • 2019-12-30
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    相关资源
    最近更新 更多