c - 变量没有正确递增

标签 c increment

我想在我的 distCasas 数组中添加数字 (nums),但 idx 递增错误。我在索引中保留 nums

0, 2, 4, ... 

不是

0, 1, 2, ...

代码:

int numCasas, idx, distCasas[10], nums;

scanf("%d", &numCasas);

for (idx = 0; idx < numCasas; idx++)
{
    scanf("%d", &nums);

    distCasas[idx] = nums;
    idx++;
}

最佳答案

您将 idx 递增两次。删除第二个 idx++,以便您的代码如下所示。

scanf("%d", &numCasas);

for (idx = 0; idx < numCasas; idx++)
{
    scanf("%d", &nums);

    distCasas[idx] = nums;
}

关于c - 变量没有正确递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23273945/

相关文章:

c - 由进程停止或 App Nap 触发的线程挂起检测器

c - 如何将文件中的字符转换为ascii整数转换为数组?

c - 矩阵行列式。错误输出

c++ - 当将预增量和后增量放在像这样的单个变量上时(++i)++,它在 c++ 中有效,但在 c 中无效

php - 在递增时在同一输出行上回显整数?

c - 有没有办法创建性别评估

java - for 循环中的 i++ &++i 有什么区别?

list - 使用 prolog 枚举子列表

Bash 增量破坏脚本

c - 什么是 int cost[N] 在 vi​​sual basic 中