这个程序看不太懂

ylj103037496 2008-11-16 05:55:08
/*showchat2.c 按行和列打印字符*/
#include <stdio.h>
#include <stdlib.h>
void display(char cr,int lines,int width);
int main()
{
int ch;//要打印的行数
int rows,cols;//行数和列数
printf("Enter a character and two integers:\n");
while((ch=getchar())!='\n')
{
if(scanf("%d %d",&rows,&cols)!=2)
break;
display(ch,rows,cols);
while(getchar()!='\n')
continue;
printf("enter another character and two integers:\n");
printf("enter a newline to quit.\n");
}
printf("Bye.\n");system("pause");
return 0;
}

void display(char cr,int lines,int width)
{
int row,col;
for(row=1;row<=lines;row++)
{
for(col=1;col<=width;col++)
putchar(cr);
putchar('\n');

system("pause");
}
}


重要的是这一块是怎么运行的:
while((ch=getchar())!='\n')
{
if(scanf("%d %d",&rows,&cols)!=2)
break;//这个语句不是控制下面的不用再运行了吗
display(ch,rows,cols);
while(getchar()!='\n')
continue;//这个是不是跳出了上面这个while
printf("enter another character and two integers:\n");
printf("enter a newline to quit.\n");
}

还有,就是为什么输入字符后,要输入空格,再输入数字,才有效?????
能否全面一点给我讲一下
...全文
94 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
太乙 2008-11-16
  • 打赏
  • 举报
回复
重要的是这一块是怎么运行的: 
while((ch=getchar())!='\n') //读入字符,一个一个读入
{
if(scanf("%d %d",&rows,&cols)!=2) //输入的时候有格式控制,如果这里改为"%d,%d"那么输入的时候就是按照,来分割!
break;//这个语句得是if成立的时候才执行
display(ch,rows,cols);
while(getchar()!='\n')
continue;//这个是紧挨着它的那个循环语句,这里就是上面一行的这个while
printf("enter another character and two integers:\n");
printf("enter a newline to quit.\n");
}

Non_Recursive 2008-11-16
  • 打赏
  • 举报
回复
up,代码有点乱。。。
qqwx_1986 2008-11-16
  • 打赏
  • 举报
回复
while((ch=getchar())!='\n')
{
if(scanf("%d %d",&rows,&cols)!=2)
break;//跳出while循环
display(ch,rows,cols);
while(getchar()!='\n')
continue;//继续上面的while循环知道getchar()=='\n',continue;可以去掉直接加个;就可以了
printf("enter another character and two integers:\n");
printf("enter a newline to quit.\n");
}

69,364

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧