0
獲取卡在有效= 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循環。 –