随机数,if,else if

时间:2012-11-23 23:06:34

标签: javascript

我正在编写这样的JavaScript代码:

function rollTrigger() {
var roll = Math.round(Math.random()*999999999+1);
var result = document.getElementById("result");

if (roll <= 50900000) {
    result.innerHTML = "Category 0";
    BoLPamount += 1;
    document.getElementById("boxlesserprizes").innerHTML = "Box of Lesser Prizes " + BoLPamount; 
    log.innerHTML += "<br />Trigger - win = true; Category 0"; //LOG        
} else if (roll <= 900000) {
    result.innerHTML = "Category A";
    log.innerHTML += "<br />Trigger - win = true; Category A"; //LOG
} else if (roll <= 360000) {
    result.innerHTML = "Category B";
    log.innerHTML += "<br />Trigger - win = true; Category B"; //LOG
} else if (roll <= 211890) {
    result.innerHTML = "Category C";
    log.innerHTML += "<br />Trigger - win = true; Category C"; //LOG
} else if (roll <= 109020) {
    result.innerHTML = "Category D";
    log.innerHTML += "<br />Trigger - win = true; Category D"; //LOG
} else if (roll <= 56505) {
    result.innerHTML = "Category E";
    log.innerHTML += "<br />Trigger - win = true; Category E"; //LOG
} else if (roll <= 34888) {
    result.innerHTML = "Category F";
    log.innerHTML += "<br />Trigger - win = true; Category F"; //LOG
} else if (roll <= 15574) {
    result.innerHTML = "Category G";
    log.innerHTML += "<br />Trigger - win = true; Category G"; //LOG
} else {
    result.innerHTML = "MEH";
    log.innerHTML += "<br />Trigger - win = false"; //LOG
};
document.getElementById("roll").innerHTML = roll;

};

但它不能正常工作......例如,如果随机数为40200000,那么它会显示“类别0”,但即使随机数为10,它仍显示“类别0”而不是“类别” G”。任何想法都错了吗?

3 个答案:

答案 0 :(得分:5)

if (roll <= 50900000)
10井小于50900000,不是吗? 颠倒if语句的顺序,一切都应该没问题。

答案 1 :(得分:2)

因为数字10将进入第一个条件,因为if(10 <= 50900000)为真。

您必须指定范围。

if(roll > 900000 && roll <= 50900000)

或从较低的条件开始直到最高

 if (roll <= 15574) 
 {
    result.innerHTML = "Category F";
    log.innerHTML += "<br />Trigger - win = true; Category F"; //LOG
 }
 else if (roll <= 34888) 
   {
     ...

等等。

答案 2 :(得分:2)

您必须订购if测试,以便它们按正确顺序排列。如果它们都使用<=,那么您需要首先比较最低值然后再比较下一个更高的值,依此类推。否则,它会匹配比您想要的更高的数字。

就个人而言,我建议使用表驱动方法,而不是进行大量if/else测试。

var rollTests = [
    {value: 15574, resultHTML: "Category G", logHTML: "<br />Trigger - win = true", logHTML: "Category G"},
    {value: 34888, resultHTML: "Category F", logHTML: "<br />Trigger - win = true", logHTML: "Category F"},
    // the rest of the values here ordered by the comparison value ....
];

for (var i = 0, len = rollTests.length; i < len; i++) {
    if (roll <= rollTests[i].value) {
        result.innerHTML = rollTests[i].resultHTML;
        log.innerHTML = rollTests[i].logHTML;
        break;
    }
}

甚至更紧凑:

var rollTests = [
    {value: 15574, category: "G"},
    {value: 34888, category: "F"},
    // the rest of the values here ordered by the comparison value ....
];

var logHTML;    
for (var i = 0, len = rollTests.length; i < len; i++) {
    if (roll <= rollTests[i].value) {
        logHTML = "Category " + rollTests[i].category;
        log.innerHTML = logHTML;
        result.innerHTML = "<br />Trigger - win = true; " + logHTML;
        break;
    }
}