C# 从循环的数组中获取下一个数据块

C# 从循环的数组中获取下一个数据块,c#,C#,我将温度数据存储在一个数组中,但需要将该数据用于一个while循环。到目前为止,我得到的是: public int BelowValueCounter(string tempValueIn) { int.TryParse(tempValueIn, out tempValueOut); int checkValue = tempData[0]; while (tempValueOut > checkValue)

我将温度数据存储在一个数组中,但需要将该数据用于一个while循环。到目前为止,我得到的是:

      public int BelowValueCounter(string tempValueIn)
      {

        int.TryParse(tempValueIn, out tempValueOut);
        int checkValue = tempData[0];  
        while (tempValueOut > checkValue)
        {
            belowCounter++;
        }
        return belowCounter;

      }

我只是不知道如何递增
tempData[0]
,这样它就会转到
tempData[1]
重复,直到满足while循环条件。谢谢

您可以使用for循环、foreach循环或linq查询

void Main()
{
    var counter = BelowValueCounter_UsingFor(46);
    //var counter = BelowValueCounter_UsingLinq(46);
    Console.WriteLine(counter); 
}

decimal[] temperatures = new decimal[] { 40, 40, 45, 60, 70 };

public int BelowValueCounter_UsingLinq(decimal tempValueIn)
{
    return temperatures.Count(a => a < tempValueIn);    
}

public int BelowValueCounter_UsingFor(decimal tempValueIn)
{
    int counter = 0;
    for (int i = 0; i < temperatures.Length; i++)
    {
        if (temperatures[i] < tempValueIn)
            counter++;
    }

    return counter; 
}
void Main()
{
var计数器=低于(46)所用的数值计数器;
//var计数器=低于使用LINQ(46)的值计数器;
控制台。写线(计数器);
}
十进制[]温度=新的十进制[]{40,40,45,60,70};
public int低于value counter_使用LINQ(十进制tempValueIn)
{
返回温度.计数(a=>a
如果要保持while循环,则需要一个用于计数的变量-此处为
i
-以访问所需的数组条目:

public int BelowValueCounter(string tempValueIn)
{
    int.TryParse(tempValueIn, out tempValueOut);
    int i = 0;
    int checkValue = tempData[i];  

    while (tempValueOut > checkValue)
    {
        belowCounter++;
        i++;
        checkValue = tempData[i];
    }

    return belowCounter;
  }

或考虑使用for循环:

public int BelowValueCounter(string tempValueIn)
{
    int.TryParse(tempValueIn, out tempValueOut);

    for (int i = 0; i < tempData.Length; i++)
    {
        if (tempValueOut > tempData[i])
        {
            belowCounter++;
            continue;
        }

        break;
    }

    return belowCounter;
}
public int BelowValueCounter(字符串tempValueIn)
{
int.TryParse(tempValueIn,out tempValueOut);
for(int i=0;itempData[i])
{
低于计数器++;
继续;
}
打破
}
返回计数器下方;
}

您是否考虑过for循环?使用当前代码,您需要定义一个变量,您可以增加每个循环。注意不要超过数组的结尾。对于(int i,itempValueIn看起来如何?@dram c#!=java@astidham2003谢谢,查看了foreach循环并使其工作!