为什么不能循环??

来源:百度知道 编辑:UC知道 时间:2024/09/24 04:56:16
#include <stdio.h>
#include <stdlib.h>
int main()
{ while(1)
{
char s[10];
int i;
gets(s);
for (i=0; i<10; i++)
{
if (s[i]=='d')
{
printf(" gets 0");exit(0);
}
else if (s[i]=='r' && s[i+1]=='r')
{
printf(" gets 0");exit(0);
}
else
{
int j ;
j=strlen(s);
printf(" gets %d ",j*4);exit(0);
}

}
}
}
为什么不能循环?如果都去掉printf();后的exit(0);却可以,但是输出10个"get %d"(%d代表得数)~~~~为什么啊?
不懂啊~

你把所有的exit(0)改成break就可以了,完整的代码如下:
主要是你用exit(0)就直接退出main函数了,用break就可以直接从for循环出来到while循环。我也不知道你这段代码项干什么,其他问题你自己去解决吧。

#include<stdio.h>
#include <stdlib.h>
#include<string.h>
void main()
{
while(1)
{
char s[10];
int i;
gets(s);
for (i=0; i<10; i++)
{
if (s[i]=='d')
{
printf(" gets 0");
break;
}
else if (s[i]=='r' && s[i+1]=='r')
{
printf(" gets 0");
break;
}
else
{
int j ;
j=strlen(s);
printf(" gets %d ",j*4);
break;

}

}
}
}