0
感谢您查看此页面。我是C#的新手,我想知道为什么这段代码会增加值。本质上,我期望测量系统中单个粒子的速度,但是,文本文件的结果显示速度的每个值都会添加到下一个。我需要的是将速度的每个值计数到一个bin中,例如,0到0.5之间的速度计数到1进入bin 1,0.5到1.0进入bin 2,等等,然后每个值bin可以写入一个文本文件,然后我可以变成一个直方图来查看某个bin中每个速度的频率。我的一段代码如下。我非常感谢我获得的任何帮助。感谢您查看该页面。这段代码为什么增加? C#
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语法。 –
什么问题?您的标题说明它会增加,并且您的描述将描述您想要递增箱来计算每个速度组的频率。这里没有问题的定义。 – Ian