请原谅我这个愚蠢的问题,但我无法解决这个问题。
我怎样才能让事情发生every third time
?
时间(左数):
shipmentId = 2 ....
int occurrence = 0;
int counter = 0;
foreach (var el in elmOrderData)
{
if (el.Name == "shipmentIndex")
{// we are entering here for every element that his name is "shipmentIndex"
el.SetValue(shipmentId);
secondTime++;
}
if ((secondTime % 2) == 0)
{// every third time we see "shipmentIndex"
secondTime = 1;
shipmentId++;
}
}
答案 0 :(得分:0)
您可以使用bool,如以下示例所示。这将显示消息框1,3,5,7和9:
bool test = false;
for (int i = 0; i < 10; i++)
{
if (test)
MessageBox.Show(i.ToString());
test = !test;
}
答案 1 :(得分:0)
尝试拼凑你的笔记 - 无论是否每次都会发生。怎么样?
int occurrence = 0;
int counter = 0;
foreach(var a in list)
{
if(some_condition)
{
// do something..
occurrence++
if(occurrence % 2 == 0)
{
counter++
}
}
}
答案 2 :(得分:0)
为什么不只是每次增加而只是除以2?
for(var i = 0; i<20; i++)
{
var j = i / 2; // or bit shift
//do work on j instead of i
}