试图找到平均值
问题描述:
我需要知道为什么当我尝试将学生1,学生2和学生3分开时。我认为这可能是一个循环错误,因为它给我的数字是数字,但是我不知道看不出会发生什么。试图找到平均值
function averageOfThreeScores() {
var student1;
var student2;
var student3;
var end;
do {
student1 = prompt("What are the scorces of students 1?");
student2 = prompt("What are the scorces of students 2?");
student3 = prompt("What are the scorces of students 3?");
end = prompt("Would you like to end, type yes to end.");
var average = (student1 + student2 + student3)/3;
if (average <= 59) {
document.write(average + " Your score is F <br/>");
} else if (average <= 69) {
document.write(average + " Your score is D <br/>");
} else if (average <= 79) {
document.write(average + " Your score is C <br/>");
} else if (average <= 95) {
document.write(average + "That's a great score <br/>");
} else if (average <= 100) {
document.write(average + "God like </br>");
} else {
document.write(average + " End <br/>");
}
}
while (end != "yes");
}
答
你期望学生成绩的总和是数字,但实际上它们是连接在一起的字符串。因此,如果按照对实施例的值的用户类型: 为student1:13 为STUDENT2:36 为学生三:50
平均将是44550,因为总和将(连接的字符串):133650
要解决这个,只要将它转换成数字即可。
student1 = parseInt(prompt("What are the scorces of students 1?"));
student2 = parseInt(prompt("What are the scorces of students 2?"));
student3 = parseInt(prompt("What are the scorces of students 3?"));
+0
谢谢Areca解决了我的问题。 – QuestionTornado
您至少需要'(student1 + STUDENT2 +学生三)/ 3;'代替'student1 + STUDENT2 +学生三/ 3;'对于操作顺序,乘法优先于加法。 –
平均值是一个数字。尝试average.toString() – lamirap
@matthew Gunn我尝试过,但它仍然不断给我我高达2200分等等,我正在寻找它分三个学生3,以找到平均。 – QuestionTornado