简体   繁体   English

If 语句中的检查问题

[英]Problem with a checking inside a If statement

Hello guys I have a problem with an If statement.大家好,我对 If 语句有疑问。 I have 2 checking inside of it and I don't know why one of them is not working.我有 2 个在里面检查,我不知道为什么其中一个不工作。 Sorry if my code is not the best but a beginner and I'm trying to improve thank you very much.抱歉,如果我的代码不是最好的而是初学者,我正在努力改进,非常感谢。

this is the one else if (command == 'hit' && p3 > 0)这是else if (command == 'hit' && p3 > 0)


if (command == `bj`){

        var p1 = 0;
        var p2 = 0;
        
        const Boolean = true;

        p1 = Math.floor((Math.random() * 11) + 1);
        p2 = Math.floor((Math.random() * 11) + 1);
        message.channel.send(`First card: ` + p1);
        message.channel.send(`Second card: ` + p2);
        var p3 = p1 + p2;
        message.channel.send(`total : ` + p3);

    } else if (command == `hit` && p3 > 0){

        message.channel.send(`checking worked !`);

    } else {
            message.channel.send(`Not Working!`);
    }

You are correct about the problem: the code inside } else if (command == 'hit' && p3 > 0){ will never run.您对这个问题是正确的: } else if (command == 'hit' && p3 > 0){中的代码将永远不会运行。

This is because of the way if/else statements work in Javascript.这是因为 if/else 语句在 Javascript 中的工作方式。 The interpreter looks at the condition, and if the condition is true, the interpreter executes the code inside that block.解释器查看条件,如果条件为真,解释器执行该块内的代码。 If the condition is false, the interpreter skips that code block completely and moves to the next condition.如果条件为假,解释器将完全跳过该代码块并移至下一个条件。

So, in your code, when command === 'bj' :因此,在您的代码中,当command === 'bj'时:


if (command == `bj`){ // command is 'bj', run this code block, skip the other cases

        var p1 = 0;
        var p2 = 0;
        
        const Boolean = true;

        p1 = Math.floor((Math.random() * 11) + 1);
        p2 = Math.floor((Math.random() * 11) + 1);
        message.channel.send(`First card: ` + p1);
        message.channel.send(`Second card: ` + p2);
        var p3 = p1 + p2;
        message.channel.send(`total : ` + p3);

    } else if (command == `hit` && p3 > 0){ // this block doesn't run, it's skipped

        message.channel.send(`checking worked !`);

    } else { // this block doesn't run, it's skipped
            message.channel.send(`Not Working!`);
    }

Now, in your code, when command === 'hit' :现在,在您的代码中,当command === 'hit'时:

if (command == `bj`){ // command is 'hit', skip this code block completely

        var p1 = 0;
        var p2 = 0;
        
        const Boolean = true;

        p1 = Math.floor((Math.random() * 11) + 1);
        p2 = Math.floor((Math.random() * 11) + 1);
        message.channel.send(`First card: ` + p1);
        message.channel.send(`Second card: ` + p2);
        var p3 = p1 + p2;
        message.channel.send(`total : ` + p3);

    } else if (command == `hit` && p3 > 0){ // command is 'hit', but p3 has no value -- the block in which it's given a value is never run!

        message.channel.send(`checking worked !`);

    } else { // this block doesn't run, it's skipped
            message.channel.send(`Not Working!`);
    }

To get the results you want, you need to re-think how to accomplish this.要获得您想要的结果,您需要重新考虑如何实现这一目标。

Since you declared p2 in a separate statement, it will not be readable in the other if statement.由于您在单独的语句中声明了p2 ,因此在另一个 if 语句中将无法读取它。 If you want to update a variable, use let eg let p2 = Math.floor((Math.random() * 11) + 1);如果要更新变量,请使用let例如let p2 = Math.floor((Math.random() * 11) + 1);

If you would benefit from a guide on Javascript, I would suggest reading this one https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide .如果您从 Javascript 指南中受益,我建议您阅读这篇https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide

Also, instead of using an else if off the main statement, why not use 2 if statements?另外,不要在主语句之外使用else if ,为什么不使用 2 if 语句呢? It achieves the effect you want.它达到了你想要的效果。 Here is an example:这是一个例子:

if (command == `bj`){
        let commandState = 1 //state variable to allow the hit command to know whether the bj command has been called (since hit is dependent on bj)
        var p1 = 0;
        var p2 = 0;
        
        const Boolean = true;

        p1 = Math.floor((Math.random() * 11) + 1);
        p2 = Math.floor((Math.random() * 11) + 1);
        message.channel.send(`First card: ` + p1);
        message.channel.send(`Second card: ` + p2);
        let p3 = p1 + p2;
        message.channel.send(`total : ` + p3);
} 


if (command == 'hit'){
       if (!commandState) return; //If the command state is not 1, or true, leave the statement
       if (p3 > 0) {
           message.channel.send("Success")
       } else {
           message.channel.send("Failure")
       }
}

You also seem to be using backticks as string operators.您似乎还使用反引号作为字符串运算符。 Backticks are only used when you need to use embedded information in your string eg (`${message.author.id}`) Using normal quotation marks will work well in all other cases - pick which you like ("example 1", 'example 2')仅当您需要在字符串中使用嵌入信息时才使用反引号,例如 (`${message.author.id}`)示例 2')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM