【发布时间】: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