c# - 检查函数连续返回值的次数

标签 c#

检查函数连续返回值的次数的最佳方法是什么?例如,在循环中运行此函数:

 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/

相关文章:

c# - 如何在 C# 中使用自动完成获取事件 "item Selected"?

c# - 重载与泛型参数

c# - System.Drawing.Common.Bitmap 跨平台替代品

c# - 应用程序如何验证来自 Azure Active Directory 的经过身份验证的 token ?

c# - 以编程方式加载程序集及其依赖项时的奇怪行为

c# - 使用 LinkBut​​ton 时禁用验证控件

c# - 如何在 C# 上停止 foreach 循环

c# - System.Threading.Timer最大 dueTime 值问题

c# - 在Binder主持的Jupyter上设置C#和F#笔记本

c# - 如何在网页浏览器控件中屏蔽 flash 元素