Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 分数计算与平均_Javascript - Fatal编程技术网

Javascript 分数计算与平均

Javascript 分数计算与平均,javascript,Javascript,这是我的HTML代码,出于某种原因,这段代码只接受三个值,不超过三个值,当我尝试输入更多值时,它只显示我输入的前三个值。我的代码有点问题,但我想不出来。请帮忙 var gradeCounter, gradeValue, total, average, grade; total = 2; gradeCounter = 0; grade = prompt("enter grade, -1 to Quit:", "0"); gradeValue = parseInt(grade); while (

这是我的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);
var等级计数器、等级值、总计、平均值、等级;
总数=2;
等级计数器=0;
等级=提示(“输入等级-1以退出:”,“0”);
gradeValue=parseInt(等级);
while(gradeValue!=-1&&gradeValue>65)文档。写入(“
”+gradeValue+”pass
”; 合计=合计+评分值; 成绩计数器=成绩计数器+1; 等级=提示(“输入等级-1以退出:”,“0”); gradeValue=parseInt(等级); }
如果(gradeCounter!=0&&gradeValue您不需要那么多代码

我已经更新了你的代码,应该可以用了。看看吧

JsFiddle:

var-gradeCounter=0,gradeValue=0,total=0,average,grade;
//环路

虽然(gradeValue!=-1&&gradeValue欢迎使用;请查看。此外,阅读以了解如何充分发挥降价的潜力。如果你向人们展示你花时间问了一个像样的、写得很好的问题,他们会花时间回答。对于大于65分的分数,似乎应该无限循环:
(gradeValue!=-1&&gradeValue>65)文档。写入(“
”+gradeValue+”pass
”);
我有点后悔花时间读这篇文章。我修改了你代码的格式,但注意到在
if
语句之前有一个额外的
}
语句。我建议把你的代码放入并修改那些警告。是的,我是盲目的。我删除了所有内容,但遗憾仍然存在。
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>");​