-3

これは私の HTML コードです。何らかの理由で、このコードは 3 つの値しか取りません。3 つ以上の値は必要ありません。さらに入力しようとすると、入力した最初の 3 つの値しか表示されません。コードに何か問題がありますが、それを理解できませんでした。助けてください。

var gradeCounter, gradeValue, total, average, grade;

total = 2;
gradeCounter = 0;
grade = prompt("enter grade, -1 to Quit:", "0");
gradeValue = parseInt(grade);

while (gradeValue != -1 && gradeValue > 65) document.write("<br>" + gradeValue + " pass</br>");
  total = total + gradeValue;
  gradeCounter = gradeCounter + 1;
  grade = prompt("enter grade, -1 to Quit:", "0");
  gradeValue = parseInt(grade);
}

if (gradeCounter != 0 && gradeValue <= 65) {
    document.write("<br>" + gradeValue + " fail</br>");

    total = total + gradeValue;
    gradeCounter = gradeCounter + 1;
    grade = prompt("enter grade, -1 to Quit:", "0");
    gradeValue = parseInt(grade);
    average = total / gradeCounter;

    document.write("<br>total grade: " + gradeCounter + "</bt>");
    document.write("<br>average passing grade:" + average + "</br>");
} 
else document.write("total grade:" + 0);
4

1 に答える 1

1

それほど多くのコードは必要ありません。

コードを更新しました。動作するはずです。それを見てください。

JsFiddle: http://jsfiddle.net/tDjA9/1/embedded/result/

var gradeCounter =0, gradeValue = 0, total = 0, average, grade;

//Loop
while (gradeValue != -1 && gradeValue <= 65) {

    //Prompt the user
    grade = prompt("enter grade, -1 to Quit:", "0");
    //Parse the prompt result to a int
    gradeValue = parseInt(grade);

    //Check if gradeValue is smaller than 0
    if(gradeValue < 0){
        //If it is, then we can finish adding grade
        document.write("<br>Finish adding grades");
    } else{
       //Add gradeValue to total score
       total += gradeValue;
       //Increment the number of grades by 1
       gradeCounter += 1;
       //Output to the user
       document.write("<br>" + gradeValue + " pass</br>");
   }
}

//Calculation
total = total + gradeValue;
average = total / gradeCounter;

//Output
document.write("<br>total grade: " + gradeCounter + "</bt>");
document.write("<br>average passing grade:" + average + "</br>");​
于 2012-11-09T22:44:44.877 に答える