检查函数连续返回值的次数
本文关键字:返回值 函数 连续 检查 | 更新日期: 2023-09-27 18:25:05
检查函数连续返回值的次数的最佳方法是什么?例如,在循环中运行此函数:
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
}