C语言,哪位高手帮我看看下面的一道C语言问题,谢谢。
为什么当我就输按一个回车符时,运行结果为:???????lines=1words=1chars=1从这个运行结果来看,编译器把回车符当做了空格('')和制表符('\t')...
为什么当我就输按一个回车符时,运行结果为: ???????
lines = 1
words =1
chars =1
从这个运行结果来看,编译器把回车符当做了空格(' ')和制表符 ( '\t' ),请问是这样的吗?????????????????????
#include <stdio.h>
void main()
{
int lines = 0,words = 0,chars = 0 ,ch;
while(1)
{
switch( ch = getchar() )
{
case '\n': lines++;
case ' ': /* fall thru */
case '\t':words += 1;
default:
chars += 1;
}
if(ch == '\n')
{
break;
}
}
printf("lines = %d\n",lines);
printf("words = %d\n",words);
printf("chars = %d\n",chars);
} 展开
lines = 1
words =1
chars =1
从这个运行结果来看,编译器把回车符当做了空格(' ')和制表符 ( '\t' ),请问是这样的吗?????????????????????
#include <stdio.h>
void main()
{
int lines = 0,words = 0,chars = 0 ,ch;
while(1)
{
switch( ch = getchar() )
{
case '\n': lines++;
case ' ': /* fall thru */
case '\t':words += 1;
default:
chars += 1;
}
if(ch == '\n')
{
break;
}
}
printf("lines = %d\n",lines);
printf("words = %d\n",words);
printf("chars = %d\n",chars);
} 展开
2个回答
展开全部
//加break可以解决问题!
#include <stdio.h>
void main()
{
int lines = 0,words = 0,chars = 0 ,ch;
while(1)
{
switch( ch = getchar() )
{
case '\n': lines++;break;
case ' ': /* fall thru */
case '\t':words += 1;break;
default:
chars += 1;
}
if(ch == '\n')
{
break;
}
}
printf("lines = %d\n",lines);
printf("words = %d\n",words);
printf("chars = %d\n",chars);
}
#include <stdio.h>
void main()
{
int lines = 0,words = 0,chars = 0 ,ch;
while(1)
{
switch( ch = getchar() )
{
case '\n': lines++;break;
case ' ': /* fall thru */
case '\t':words += 1;break;
default:
chars += 1;
}
if(ch == '\n')
{
break;
}
}
printf("lines = %d\n",lines);
printf("words = %d\n",words);
printf("chars = %d\n",chars);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询