Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# For循环每4个实例一次,但也要跟踪总计数值_C#_For Loop - Fatal编程技术网

C# For循环每4个实例一次,但也要跟踪总计数值

C# For循环每4个实例一次,但也要跟踪总计数值,c#,for-loop,C#,For Loop,下面的代码在有“邮箱”时生成一个“签名框”,然后每4个“邮箱”增加一个“签名框” 我的问题是我需要根据“邮箱”计数计算“签名重量”,并每4个“邮箱”重置一次。但由于循环只经历一次增量。我记不住“邮箱”的总数 注意:下面的代码是实际代码的简单演示 double qPostBox = 3.0; // for example I have 3 post boxes int signCount = 0; int postCount = 0; for (int i = 0; i < qPostBox

下面的代码在有“邮箱”时生成一个“签名框”,然后每4个“邮箱”增加一个“签名框”

我的问题是我需要根据“邮箱”计数计算“签名重量”,并每4个“邮箱”重置一次。但由于循环只经历一次增量。我记不住“邮箱”的总数

注意:下面的代码是实际代码的简单演示

double qPostBox = 3.0; // for example I have 3 post boxes
int signCount = 0;
int postCount = 0;
for (int i = 0; i < qPostBox; i++)
{
    postCount++;
    if (i % 4 == 0)
    {
        signCount++;
        Console.WriteLine("SIGN BOX# " + signCount + " SIGN WEIGHT: " + postCount * 4);
        postCount = 1;
    }
    Console.WriteLine("POST BOX# " + postCount);
}
期望输出:

SIGN BOX# 1 SIGN WEIGHT: 12
POST BOX# 1
POST BOX# 2
POST BOX# 3

你的问题在于你做事的顺序

数一数箱子,然后显示总重量

int postBoxCount = 5;
int signCount = 0;
int postCount = 0;

for (int i = 0; i < postBoxCount; i++)
{
    // don't show the total at the very beginning (when i == 0)
    if (i != 0 && i % 4 == 0)
    {
        signCount++;
        Console.WriteLine("SIGN BOX# " + signCount + " SIGN WEIGHT: " + postCount * 4);
        postCount = 0;
    }
    postCount++;
    Console.WriteLine("POST BOX# " + postCount);
}

// show the final total
signCount++;
Console.WriteLine("SIGN BOX# " + signCount + " SIGN WEIGHT: " + postCount * 4);
intpostboxcount=5;
int signCount=0;
int postCount=0;
对于(int i=0;i

“我记不住‘邮箱’的总数了”——当然可以。添加另一个变量。邮箱数必须为整数。邮箱编号=(i%邮箱编号)+1;邮箱数量=(i/邮箱数量)+1@EdPlunkett如果有3个邮箱,if会投上4重的标志,就这样。我想让if知道当它在if中时有多少个盒子。如果你知道我在说什么mean@EvikGhazarian我不明白你的意思。我认为你需要更清楚地解释你的要求。目前还不清楚“标志重量”与“邮箱”第一个问题的关系。这不适用于低于4的邮箱或每4个邮箱之间的剩余邮箱。例如,如果其为6,则应为2标志框。一个砝码16,另一个砝码8。这有可能吗?自从我第一次发布以来,我已经对它进行了更新,因为我发现了确切的东西。如果(i==1)
结尾的逻辑是胡说八道。循环总是以显示邮箱计数结束,因此我将其更改为循环结束时总是显示总数。我还更新了dotnetfiddle示例。玩一玩,确保它如预期的那样。再次感谢:)这再好不过了。
int postBoxCount = 5;
int signCount = 0;
int postCount = 0;

for (int i = 0; i < postBoxCount; i++)
{
    // don't show the total at the very beginning (when i == 0)
    if (i != 0 && i % 4 == 0)
    {
        signCount++;
        Console.WriteLine("SIGN BOX# " + signCount + " SIGN WEIGHT: " + postCount * 4);
        postCount = 0;
    }
    postCount++;
    Console.WriteLine("POST BOX# " + postCount);
}

// show the final total
signCount++;
Console.WriteLine("SIGN BOX# " + signCount + " SIGN WEIGHT: " + postCount * 4);