我在使用提示符和计数器运行 JavaScript while 循环时遇到问题

发布于 2025-01-11 17:25:52 字数 853 浏览 0 评论 0原文

在我的程序中,我首先有一个提示,要求用户输入成绩,然后是一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱暴击 2025-01-18 17:25:52

对于您的问题,您需要使用 Do While 而不是 While else...

Do { This } while {grade = -1}

不了解您的学期平均计算技术。希望下面的代码能够解决您的问题。谢谢

var count = 0;
var total = 0;
var grade;
var avg = 0;


do { 
grade = prompt('Enter grade(-1 to stop)');
count++;  
document.write('<table><tr>');
document.write('<td>Grade ' + count  + ': </td>');
document.write('<td width="50"> ' + grade + ' </td>');
document.write('</tr>');


total = total + grade;    
document.write('</table>');
avg = total / count;
document.write('Semester Average: ' + avg +'<br /><br />');
}
while (grade != -1) {
    --total;
    alert("You have input -1 to stop");
 }

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

var count = 0;
var total = 0;
var grade;
var avg = 0;


do { 
grade = prompt('Enter grade(-1 to stop)');
count++;  
document.write('<table><tr>');
document.write('<td>Grade ' + count  + ': </td>');
document.write('<td width="50"> ' + grade + ' </td>');
document.write('</tr>');


total = total + grade;    
document.write('</table>');
avg = total / count;
document.write('Semester Average: ' + avg +'<br /><br />');
}
while (grade != -1) {
    --total;
    alert("You have input -1 to stop");
 }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文