#include<stdio.h>
int main()
{ int x,i=0,l=0;
float s=0,q=0,w,e;
printf("输入一个数: ");
scanf("%d",&x);
while(x)
{
if (x%2==0)
{s=s+x;i++;}
else {q=q+x; l++;}}
if (i!=0)
w=s/i;
else w=0;
if(l!=0)
e=q/l;
else e=0;
printf("%f%f",w,e);
return 0;
}
你的程序有死循环,可以描述下你的程序是在干什么吗?
你这个while循环是没办法跳出去的,可以去看看这个循环是怎么用的
这段里死循环:
while (x)
{
if (x % 2 == 0)
{
s = s + x;
i++;
}
else {
q = q + x;
l++;
}
}
你这样无法退出while循环,加上break即可,改正如下:
while (x)
{
if (x % 2 == 0)
{
s = s + x; i++;
break;
}
else {
q = q + x; l++;
break;
}
}