-2

我意识到这与案例有关,我该如何解决,或者您能否建议一种更好的方法来获得我想要的最终产品,任何帮助都会很好

function main()

{

    var a;
    var answer;
    a = prompt("Test Score One Out of 20", a);
    var b;
    b = prompt("Test Score Two Out of 20", b);
    var c;
    c = prompt("Test Score Three Out of 20", c);
    answer = parseInt(a,10) + parseInt(b,10) + parseInt(c,10);
    switch(answer)
    {
        case "answer >= 52":
            alert("You got an A");
            break;
        case "answer >= 42":
            alert("You got a B");
            break;
        case "answer >= 33":
            alert("You got a C");
            break;
        case "answer <= 32":
            alert("You got a D");
            break;


        default:
            alert("Failed");
            break;
    }
}
4

1 回答 1

2

而不是这个奇怪的switch块,只需使用

if (answer >= 52) {
   alert("You got an A");
} else if (answer >= 42) {
   alert("You got a B");
} else if ...

还有许多其他方法可以做到这一点,例如存储阈值的数组,但这种方法有效且简单。

于 2013-05-03T14:26:20.127 回答