2016-04-04 52 views
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); 
     } 
     } 
+0

由於Math.Round語法。 –

+0

什麼問題?您的標題說明它會增加,並且您的描述將描述您想要遞增箱來計算每個速度組的頻率。這裏沒有問題的定義。 – Ian

回答

0
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); 
     } 
     }