在valid =0 while循环中被卡住,在这个循环中,它不会传递scanf,并不断请求输入。该程序被设计为接受GPA的整数值,然后计算给定的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;
}发布于 2016-11-16 18:44:31
当我执行它时,它不会卡在while(valid == 0)循环中,它会卡在第一个while(count < 5)中,因为你不会在这个循环中增加计数。
在这段代码中,我还可以指出一些其他的东西。
int array[10] = {0}一样声明,自动将数组的所有元素设置为0。有几种方法可以做到这一点(包括for循环),但您不需要像在第11-18行那样手动键入每个方法,因为编辑:
while(valid == 0)循环之前将valid重置为0(您也可以只执行while(!valid) btw)https://stackoverflow.com/questions/40629773
复制相似问题