C#,当条件成立时计数器重置

时间:2017-11-06 11:21:41

标签: c# loops for-loop if-statement

我有两个名为a1count和b1count的计数器。 代码的一部分用于控制步进电机。 只要电机驱动器返回1,就意味着旋转周期完成并且a1count递增。 在循环期间,如果由于某种原因电机无法完成旋转(失败条件),则驱动程序返回0,并且b1count递增。

现在我试图创建一个名为c1count的连续失败条件计数,如果假设b1count达到10,则会弹出一个消息框。如果c1count< = 10,则会弹出一个消息框通知错误。但是,如果在这10个计数之间,a1count递增,那么我想将c1count重置为0,如果b1count连续达到10而没有a1count增量,则再次开始计数到10。

这就是我现在所拥有的:

public int a1count = 0;
public int b1count = 0;
public int c1count=0;

if (ethernetIPforSLCMicroCom1.Write("N7:70") == 1)
{    
    a1count += 1;        
}
else if (ethernetIPforSLCMicroCom1.Write("N7:70") == 0)
{                   
    b1count += 1;                   
}

   /* Im trying to create a consecutive fail condition count called c1count 
    which will pop-up a messagebox if say suppose b1count reaches 10. 
    If c1count <= 10 then a messagebox pops up notifying the error. 
    However, if in between those 10 counts, a1count is incremented then 
    I want to reset c1count to 0, ans start counting to 10 again if 
    b1count reaches 10 consecutively without a1count increment.*/

c1count = b1count;
if (c1count <= 10)
{                    
    MessageBox.Show("Consecutive Rejection Occured);
    ethernetIPforSLCMicroCom1.Write("B3:11/6", "1");                   
}

请帮助!!

1 个答案:

答案 0 :(得分:-3)

我不清楚你的意思,你想要这样吗?

public int a1count = 0;
public int b1count = 0;
public int c1count=0;

if (ethernetIPforSLCMicroCom1.Write("N7:70") == 1)
{    
    a1count++; 
    c1count = 0;   
}
else if (ethernetIPforSLCMicroCom1.Write("N7:70") == 0)
{                   
    b1count++;
    c1count++;                  
}


if (c1count > 0 && c1count <= 10)
{                    
    MessageBox.Show("Consecutive Rejection Occured);
    ethernetIPforSLCMicroCom1.Write("B3:11/6", "1");                   
}