0

我正在尝试制作一个程序来确定输入的成绩是通过还是失败。当用户输入 -1 时它会停止。一旦用户输入-1,它必须打印出通过分数的平均值和总分数。虽然它并没有真正正常工作。我究竟做错了什么?

var countTotal=0;   //variable to store total grades.
var countPassing=0; //variable to store total passing scores.
var x= 0;       //variable to contain sum of all passing scores.
var grade=parseInt(prompt("Please enter a grade."));

while (grade != (-1*1)) 
{
    if (grade > 65) {
        grade=parseInt(prompt("Please enter a grade."));
        document.write(grade + " is passing.<br>");
        x=x+grade;
        countPassing++;
    }
    else {
        grade=parseInt(prompt("Please enter a grade."));
        document.write(grade + " is failing.<br>");
    }

    countTotal++;
}

//Loop ends

document.write("Total # of grades: " + countTotal);         //Prints out total # of passing scores.
document.write("Passing average: " + ((x)/(countPassing))); //Prints out average of all passing scores.
4

1 回答 1

2

尝试手动完成它。用户进入第一级。然后循环开始。立即,你要求一个新的成绩,丢弃第一个。如果将提示向下移动到循环的末尾,它应该可以工作。像这样:

while (grade != (-1*1)) 
{
    if (grade > 65) {
        document.write(grade + " is passing.<br>");
        x=x+grade;
        countPassing++;
    }
    else {
        document.write(grade + " is failing.<br>");
    }

    grade=parseInt(prompt("Please enter a grade."));
    countTotal++;
}
于 2012-11-14T15:03:44.210 回答