我必须参加 C 语言大学类(class),并且我将使用 while 循环读取一些整数。代码是:
#include <stdio.h>
#define max 100
int main(){
int a[max];
int i,n;
printf("Enter the number of persons: ");
do{
scanf("%i", &n);
}while((n < 1) || (n > max));
i = 0;
while (i < n){
printf("Enter the age of the %i th Person", i+1);
scanf("%i", &a[i]);
i = i + 1;
}
/* further code */
它可以编译(使用 gcc 编译器),但是一旦我进入循环,它就会正确读取数字,但在最后一次输入之后,不再执行任何操作。
最佳答案
初始化i
int i = 0;
int n;
[编辑:我现在看到您已经按照我的建议编辑了代码]
关于C While 循环未正确退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55178864/