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

手作業で試してみてください。ユーザーは1年生に入ります。次に、ループが開始されます。すぐに、最初のグレードを破棄して、新しいグレードを要求します。プロンプトをループの最後まで移動すると、機能するはずです。このような:

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 に答える