-3

这是我的 HTML 代码,由于某种原因,这段代码只需要三个值,它不会超过三个值,当我尝试输入更多时,它只显示我输入的前三个值。我的代码有问题,但我无法弄清楚。请帮忙。

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 回答