-1

我正在编写这样的 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”。知道有什么问题吗?

4

3 回答 3

5

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

于 2012-11-23T23:09:25.860 回答
2

因为如果 (10 <= 50900000) 为真,它将进​​入第 10 个条件。

您必须指定范围。

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) 
   {
     ...

等等。

于 2012-11-23T23:09:01.600 回答
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;
    }
}
于 2012-11-23T23:10:16.753 回答