要求在输入一个整数n后,显示出从1-n所有整数,以及他们的平方。
要求是每隔24个数暂停一下,屏幕输出“press enter to continue ”----用户输入内容----程序判断用户输入的字符是否是换行,如果是,继续循环(即从25开始输出)。
#include
int main(void)
{
int i,n;
printf("This program prints a table of squares.\n");
printf("Enter a number of entries in table:");
scanf("%d",&n);
for(i=1;i<=n;i++){
printf("%10d%10d\n",i,i*i);
if(i%24==0){
printf("press enter to continue...\n");
while(getchar()!='\n')
;
}
}
return 0;
}
我运行了我的程序,第一个空格按下后,直接输出了两段,即1-24,25-48。后面的正常。应该是第一次按回车被getchar()读取了,所以输出了25-48。但是怎么改呢?谢谢各位!
初学者在C程序设计现代方法中遇到题目。请各位看下哪里的问题
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
6条回答 默认 最新
threenewbee 2018-01-26 09:51关注printf("press enter to continue...\n");
while(getchar()!='\n')
;
->
system("pause");解决 无用评论 打赏 举报