if语句触发错误输入

时间:2014-11-26 17:15:14

标签: javascript if-statement

以及我在if语句触发错误输入时遇到问题

一个例子我有一个随机数生成器,当我得到输出3 它自动激活if语句,当它获得1或5

时应该激活

代码:

var randomnumber;

function randomIntFromInterval(min,max)
{
    randomnumber = Math.floor(Math.random()*(max-min+1)+min);
    console.log(randomnumber);
}

var LTB = 0; //this is how many times you have bought a lottery ticket

Lottery.addEventListener("click", function() {
    LF(1);
});    

function LF(lottery_number){
    if (lottery_number == 1) {
        randomIntFromInterval(1, 5);
        if (randomnumber == 1 | 5) {
            Points = Points * 2;
        }else if (randomnumber == 2 | 4) {
            Points = Points + 10;
        }else if (randomnumber == 3) {
            Bitcoins = Bitcoins + 1;
        }
    }else if (lottery_number == 2) {
        //i'm gonna add something here later and i have tried removing it
}
randomnumber = 0;
}

2 个答案:

答案 0 :(得分:1)

randomnumber == 1 | 5

应该是:

randomnumber == 1 || randomnumber == 5

最终结果:

function LF(lottery_number){
    if (lottery_number == 1) {
        randomIntFromInterval(1, 5);
    if (randomnumber == 1 || randomnumber == 5) {
        Points = Points * 2;
    }else if (randomnumber == 2 || randomnumber 4) {
        Points = Points + 10;
    }else if (randomnumber == 3) {
        Bitcoins = Bitcoins + 1;
    }
}else if (lottery_number == 2) {
    //i'm gonna add something here later and i have tried removing it
}
randomnumber = 0;
}

'|'是Bitwise OR,是你不想做的事情。 ||是条件OR。这应该正确地为您提供if语句检查。

答案 1 :(得分:0)

randomnumber == 1 | 5randomnumber == 1 || randomnumber == 5

不同

|是一个bitwise operator,因此您的体验正在评估类似于false | 5的内容,而0 | 5仅仅是5,这是真实的所以它进入第一个if块。