【问题标题】:Nested for loop improvements嵌套 for 循环改进
【发布时间】:2016-11-14 17:35:36
【问题描述】:

我正在尝试制作一个包含 最多 63 个 位置的数组的程序。用户指定数组的实际活动位置,所有位置的起始值都是 0。

我想: 扫描数组的活动位置以及位于 0+4*n 和 3+4*n 的位置(其中 n=0,1,2,3,4, ((total-5)/4) 检查如果值为 0,如果是,则设为 1。 对于未包含在先前搜索中的数组的最后 5 个活动位置,我将只检查数组的五个位置中的第一个和最后一个。如果值为 0,则将其设为 1。 在所有情况下,我一次只想更改 一个值。如果找到零,则给出值 1,然后停止。 如果上面的值都不是 0 -> 有错误消息。

我的问题在最后一部分,错误消息在其他 if 语句激活时开始运行..

#include <stdio.h>
int main(void)
{
    int array[63];
    int i,n,total;
    char selection;

    for (i=0; i<63; i++)
        array[i] = 0;
    printf("Enter active array places (maximum 63):");
    scanf(" %d",&total);

    do{
        printf("0.Exit \n");
        printf("1.List array \n");
        printf("2.Change value of array: \n");
        scanf(" %c",&selection);

        if (selection=='1')
            {   
                for (i=0; i<total; i++)
                    printf("%d \n", array[i]);
            }

        else if (selection=='2')
            {
                for (i=0; i<(total); i++)
                    {
                    if ((i%4==0) && i<(total-5)&&(array[i]==0))
                                    {
                                        array[i]=1;
                                        printf("You changed array value in place %d, ",i);

                                        break;
                                    }


                                else if ((i%4==3)&&i<(total-5)&&(array[i]==0))
                                    {
                                        array[i]=1;
                                        printf("You changed array value in place %d, ",i);

                                        break;
                                    }
                                else if (i==(total-5)&&(array[i]==0))
                                    {
                                        array[i]=1;
                                        fprintf("You changed array value in place %d, ",i);
                                        i=n=total+1;
                                        break;
                                    }
                                else if (i==(total-1)&&(array[i]==0))
                                    {
                                        array[i]=1;
                                        printf("You changed array value in place %d, ",i);
                                        i=n=total+1;
                                        break;
                                    }
                                else
                                    printf("Nothing to change");

                            }
                    }
            }
    } while (selection!='0');
}

【问题讨论】:

  • 你不需要嵌套循环。只需检查i%4 == 0 || i%4 == 3
  • 谢谢!如上所示,我根据您的建议改进了我的代码。然而,最后一个 else 语句的主要问题仍然存在..我想我已经接近问题了..

标签: c arrays for-loop


【解决方案1】:

您的代码比需要的要复杂一些(请参阅@Barmar 的评论)。

但是,如果我们照原样使用您的代码,处理错误消息的一种方法是将打印退出循环并使用变量来记住是否有更改。

类似:

int item_changed = 0;  // In the start of the code before the for-loop

if ((i%4==0) && i<(total-5)&&(array[i]==0))
{
    array[i]=1;
    printf("You changed array value in place %d, ",i);
    item_changed = 1; // Add this in all 4 places where you change the array
    break;
}

删除这部分

else
    printf("Nothing to change");

并将其放在 for 循环之后。

if (item_changed == 0)
    printf("Nothing to change");

【讨论】:

  • 谢谢你们俩。有效。我不得不对您的解决方案进行小幅调整。就在 for 循环开始时,我添加了 item_changed=0;以便 item_changed 变量在每次 for 循环开始时重置为 0!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-07
  • 2017-01-06
  • 1970-01-01
  • 1970-01-01
  • 2015-01-28
  • 1970-01-01
相关资源
最近更新 更多