【发布时间】:2016-11-16 10:29:53
【问题描述】:
卡在 valid = 0 while 循环中,它没有通过 scanf 并不断要求输入。该程序旨在接受 GPA 的整数值,然后计算每个 gpa 在给定值中出现的频率。
#include <stdio.h>
int main(void) {
int amount, i, count, valid;
int GPA[200], GPAFreq[4];
valid = 0;
i = 1;
count = 1;
GPA[1] = 0; GPA[6] = 0;
GPA[2] = 0; GPA[7] = 0;
GPA[3] = 0; GPA[8] = 0;
GPA[4] = 0; GPA[9] = 0;
GPA[5] = 0; GPA[10] = 0;
GPAFreq[1] = 0; GPAFreq[3] = 0;
GPAFreq[2] = 0; GPAFreq[4] = 0;
printf("Enter the number of students: ");
scanf("%d", &amount);
while ( i < (amount + 1))
{
i += 1;
while (valid == 0)
{
printf("%d", GPA[i]);
if ( (GPA[i] == 4) || (GPA[i] == 3) || (GPA[i] == 2) || (GPA[i] == 1) )
{
valid = 1;
}
else
{
printf("GPA of student # %d is: ", i);
scanf("%d", &GPA[i]);
}
}
count = 1;
while (count < 5)
{
if (GPA[i] == count)
{
GPAFreq[count] +=1;
}
}
}
count = 1;
while (count < 5)
{
printf("\nGPA %d --- %d student(s).", count, GPAFreq[count]);
count += 1;
}
return 0;
}
【问题讨论】:
-
考虑在第三个while循环中增加“count”的值。
标签: c