这是我的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);
答案 0 :(得分: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>");