【问题标题】:Why does this piece of code increment? C#为什么这段代码会增加? C#
【发布时间】:2016-04-04 23:28:06
【问题描述】:

感谢您浏览此页面。我是 C# 新手,我想知道为什么这段代码会增加值。本质上,我希望测量系统中单个粒子的速度,但是,文本文件的结果显示速度的每个值都会添加到下一个。我需要将每个速度值的计数放入一个 bin 中,例如将 0 到 0.5 之间的速度计数 1 放入 bin 1 并将 0.5 到 1.0 放入 bin 2 等等,然后每个值bin 可以写入一个文本文件,然后我可以将其转换为直方图,以查看某个 bin 中每个速度的出现频率。我的一段代码如下。我非常感谢我得到的任何帮助。感谢您查看该页面。

private void computeVelocity()
    {
        //calc velocity of each particle for printing
        double binSize = 0.5;
        double velocityOfParticle = 0.0;
        double averageSquareVelocity = 0.0;
        int maxArrayValue = 0;
        int binArraySize = 0;
        int z;
        double[] velocityArray = new double[Particle.allParticles.Count];

        for (int counter = 0; counter < Particle.allParticles.Count; counter++)
        {
            averageSquareVelocity = Particle.allParticles[counter].SquareVelocity;
            velocityOfParticle = Math.Sqrt(averageSquareVelocity);
            velocityArray[counter] = velocityOfParticle; 
        }
        maxArrayValue = Convert.ToInt32(Math.Round(velocityArray.Max()));
        binArraySize = Convert.ToInt32(Math.Round(maxArrayValue / binSize));
        //numberOfBins = Math.Round(maxArrayValue / binSize);
        int [] binCountArray = new int[numberOfParticles];
        int incrementTerm = 0;
        Array.Clear(binCountArray, 0, binCountArray.Length);
        for(double counter = 0.0; counter <= maxArrayValue; counter = counter + binSize)
        {
            if(counter > binSize)
            {
                incrementTerm = incrementTerm + 1;
            }
            z = 0;
            for(int i = 0; i < numberOfParticles; i++)
            {
                if(velocityArray[i] <= counter)
                {
                    z++;
                }                  
            }     
            binCountArray[incrementTerm] = z;
        }
        foreach (var item in binCountArray)
        {
            VelocityValues.WriteLine(item);
        }
      }

【问题讨论】:

  • 因为 Math.Round 语法。
  • 有什么问题?您的标题说它会增加,并且您的描述描述了您希望如何增加 bin 来计算每个速度组的频率。这里没有问题定义。

标签: c# arrays increment recording bin


【解决方案1】:
if(velocityArray[i] <= counter) 

这将添加柜台下的所有内容,而不是柜台下和柜台上的binSize。更正(可能):

private void computeVelocity()
    {
        //calc velocity of each particle for printing
        double binSize = 0.5;
        double velocityOfParticle = 0.0;
        double averageSquareVelocity = 0.0;
        int maxArrayValue = 0;
        int binArraySize = 0;
        int z;
        double[] velocityArray = new double[Particle.allParticles.Count];

        for (int counter = 0; counter < Particle.allParticles.Count; counter++)
        {
            averageSquareVelocity = Particle.allParticles[counter].SquareVelocity;
            velocityOfParticle = Math.Sqrt(averageSquareVelocity);
            velocityArray[counter] = velocityOfParticle; 
        }
        maxArrayValue = Convert.ToInt32(Math.Round(velocityArray.Max()));
        binArraySize = Convert.ToInt32(Math.Round(maxArrayValue / binSize));
        //numberOfBins = Math.Round(maxArrayValue / binSize);
        int [] binCountArray = new int[numberOfParticles];
        int incrementTerm = 0;
        Array.Clear(binCountArray, 0, binCountArray.Length);
        for(double counter = 0.0; counter <= maxArrayValue; counter = counter + binSize)
        {
            if(counter > binSize)
            {
                incrementTerm = incrementTerm + 1;
            }
            z = 0;
            for(int i = 0; i < numberOfParticles; i++)
            {
                if((velocityArray[i] <= counter) && (velocityArray[i] > counter - binSize))
                {
                    z++;
                }                  
            }     
            binCountArray[incrementTerm] = z;
        }
        foreach (var item in binCountArray)
        {
            VelocityValues.WriteLine(item);
        }
      }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 2013-04-27
    • 2012-06-06
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多