gpt4 book ai didi

c - 有条件地输入数组

转载 作者:太空宇宙 更新时间:2023-11-04 05:47:28 25 4
gpt4 key购买 nike

我想将用户输入的数字输入一个数组(最多 5 个)。

一旦数组中的数字数量达到 5 或输入数字“0”,数组就会打印出输入的数字数量。

我将如何修复此代码以实现该目标?

#include <stdio.h>

void main()
{
//Declare Variables
int i,n,arr[5];

//Insert Into Array
printf("Input up to 5 integers:\n");
for(i=0; i<5; i++)
{
printf("%d : ",i+1);
scanf("%d", &arr[i]);
if (arr[i] == 0)
{
break;
}
else
{
continue;
}
}

//Display Array
printf("The %d integers stored in the array are:\n", n);
for(i=0; i<5; i++)
{
printf("%d\n", arr[i]);
}
printf("\n");
}

最佳答案

How would I fix this code to achieve that?

n = i 设置为第一个循环内的第一个语句:

  for (i = 0; i < 5; ++i)
{
n = i;

并让第二个循环运行到 n 为止:

  for (i = 0; i < n; ++i)

关于c - 有条件地输入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57346150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com