我在使用提示符和计数器运行 JavaScript while 循环时遇到问题
在我的程序中,我首先有一个提示,要求用户输入成绩,然后是一个 while 循环,表示当成绩不等于 -1 时,还有一个 if 检查成绩是否在 0 到 100 之间。我有一个 else带有一个名为“count”的计数器的语句,随后递增一,然后在表格中打印等级。然后程序再次询问成绩,直到按下 -1。
这是我的代码。
var count = 0;
var total = 0;
var grade;
var avg = 0;
grade = prompt('Enter grade(-1 to stop)');
while (grade != -1) {
if (grade < -1 || > 100) {
grade = prompt('Enter grade again');
}
else
count++;
document.write('<tr>');
document.write('<td>Grade ' + count '</td>');
document.write('<td width="50">' + grade '</td>');
document.write('</tr>');
grade = prompt('Enter grade(-1 to stop)');
total = total + grade;
document.write('</table>');
avg = total / count;
document.write('Semester Average: ' + avg);
当输入负数 1 时,
应将所有等级的总数相加,然后通过将计数除以总数来计算平均值。这不会运行。任何提示将不胜感激。
In my program I have a prompt first that asks for the user to enter a grade and then a while loop which says while the grade does not equal -1 and an if to check if the grade is between 0 to 100. I have an else statement with a counter called "count" incrementing by one afterwards and then printing the grade within a table. The program then asks for the grade again until a -1 is pressed.
Here is my code.
var count = 0;
var total = 0;
var grade;
var avg = 0;
grade = prompt('Enter grade(-1 to stop)');
while (grade != -1) {
if (grade < -1 || > 100) {
grade = prompt('Enter grade again');
}
else
count++;
document.write('<tr>');
document.write('<td>Grade ' + count '</td>');
document.write('<td width="50">' + grade '</td>');
document.write('</tr>');
grade = prompt('Enter grade(-1 to stop)');
total = total + grade;
document.write('</table>');
avg = total / count;
document.write('Semester Average: ' + avg);
}
When a negative 1 is entered it should add of the total number of all grades, and then calculate an average by dividing the count by the total. This will not run. Any tips would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于您的问题,您需要使用 Do While 而不是 While else...
Do { This } while {grade = -1}
不了解您的学期平均计算技术。希望下面的代码能够解决您的问题。谢谢
for your problem you need to use Do While instead of While else...
Do { This } while {grade = -1}
didn't understand your semester average calculating techniques. Hopefully below code will solve your problem. thanks