c - 我在修改当前代码以设置此 C 代码的不确定数量的学生时遇到了很多麻烦

标签 c for-loop nested-loops sequential repeat

     #include <stdio.h>
int main ()
{
  /* variable definition: */
  char StudentName[100];
  float ExamValue, Sum, Avg;
  int students,exams;
// Loop through 5 Students
for (students=0; students <5 ; students++) {
     // reset Sum to 0
     Sum =0.0;
     printf("Enter Student Name \n");
     scanf("%s", StudentName);
     // Nested Loop for Exams
    for (exams=0; exams < 3; exams++)
    {
        printf ("Enter exam grade: \n");
        scanf("%f", &ExamValue);
        Sum += ExamValue;
}
Avg = Sum/3.0;
printf( "Average for %s is %f\n",StudentName,Avg);
}
return 0; }

这是我需要更改的代码,以便输入具有 3 个考试成绩的 5 个学生姓名,而不会输入具有 3 个测验成绩的不确定数量的学生。我可以编写它,以便用户可以输入他们想要的学生数量,但我不认为这就是他们所说的“未确定”的意思。您可以如何编写它以便您可以输入任意数量的学生姓名。我总是愿意了解更多,并且感谢任何帮助。谢谢。

最佳答案

您可以通过用 while 循环替换 for 循环来做到这一点:

int bContinue = 1;
while (bContinue)
{
    // your code goes in here

    printf("Enter 0 if you wanna stop\n");
    scanf("%d", &bContinue);
}

关于c - 我在修改当前代码以设置此 C 代码的不确定数量的学生时遇到了很多麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39841885/

相关文章:

c - Jstring在.c语言中的比较

c - 局部变量 auto int a 和 int a 有什么区别?

Python字典逐点输出

Python for 和 if 循环不起作用?

c# - 如何从 C# 字符串中按名称查找文本框控件?

java - 没有得到想要的结果并且很困难

javascript - 嵌套的 WHILE 循环未按预期运行 - Javascript/Google Apps 脚本

并发 RPC 服务器

c - 为什么这个字符串函数的最后一个字符是 "?"

php - for & while循环条件比较