检查函数连续返回值的次数的最佳方法是什么?例如,在循环中运行此函数:
static bool F (int i) {
if (i>1) return true;
return false;
}
现在
if (function return true consecutively for 10 times ) {Do something}
else {}
我一直通过将值添加到列表中,然后检查列表的内容来做到这一点,但我想知道最专业的方法是什么。
这样程序需要很长时间才能响应。
最佳答案
你必须数一下:
int trueCount = 0;
bool wasTrue = false;
for(int i=0; i<100000; i++)
{
wasTrue = F(i);
if (wasTrue)
trueCount++;
else
trueCount = 0;
if(trueCount == 10)
;//Do Something
else
;// Do something else
}
关于c# - 检查函数连续返回值的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17896544/