c语言 while循环在不符合循环条件时循环了

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {
double a;
int x,z,b;
scanf("%d",&x);
z = x;
a = 1;
b = 0;
while (x != -1){
a += 1;
scanf("%d",&x);
z += x;
printf("a=%f,z=%d,b=%d",a,z,b);
}
b = z / a;
printf("%f\n",1.0*z/a);

}

这是运行结果
2
3
a=2.000000,z=5,b=04
a=3.000000,z=9,b=05
a=4.000000,z=14,b=0-1
a=5.000000,z=13,b=02.600000


Process exited after 38 seconds with return value 9
请按任意键继续. . .

代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {
    double b;
    int x,z,a;
    x = 0;
    z = 0;
    a = 0;
    while (x != -1){
        a += 1;
        scanf("%d",&x);
        z += x;
    }
    b = (z*1.0+1) / (a*1.0-1);
    printf("%f",b);
}


你这里呢首先输入了2,那么循环x!=1是真,所以进去循环体,循环体先计算a+=1,再输入x的值,后输出a,b,z的结果,直到你输入-1的时候退出循环