|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- int main()
- {
- int count[10];
- int i=0,x,j;
- scanf("%d",&x);
- while(getchar() != '\n')
- {
- count[i] = x;
- i++;
- scanf("%d",&x);
- }
-
- for(j=0; j<10; j++)
- {
- printf("%d\n",count[j]);
- }
- return 0;
- }
复制代码 这样子的代码,我先输入数据进数组,再打印出来,不知道为什么输入10位是显示正确,但输入5位是最后一个数却显示0,
发不了图片
我输入
1 2 3 4 5
得到的却是
1
2
3
4
0
为什么第5位是0?
本帖最后由 jhq999 于 2022-10-13 12:13 编辑
- int main() //
- {
- int count[10];
- int i=0,x,j;
- scanf("%d",&x);
- while(getchar() != '\n')
- {
- count[i] = x;
- i++;
- scanf("%d",&x);
- }
- count[i] = x;/////如果不加这个,x=5时那么因为getchar() = '\n'而退出循环,count[i] = x;不能赋值了
- for(j=0; j<i+1; j++)
- {
- printf("%d\n",count[j]);
- }
- return 0;
复制代码- #include <stdio.h>
- int main() //
- {
- int count[10];
- int i=0,x,j;
- do
- {
- scanf("%d",&x);
- count[i] = x;
- i++;
- }while(getchar() != '\n');
- for(j=0; j<i; j++)
- {
- printf("%d\n",count[j]);
- }
- return 0;
- }
复制代码
|
|