正常に動作しない単純なコードがあります。
このコードは、BigDecimalsの配列を追加して、array.lengthで除算し、平均を求めようとしています。ただし、アルゴリズムの最初のフェーズでは、配列を正しく(変数「sum」で)追加できません。
public BigDecimal getAverageHeight()
{
BigDecimal sum = new BigDecimal(0);
BigDecimal[] heights = getAllHeights();
for (int a = 0; a < heights.length; a++)
{
sum.add(heights[a]);
System.out.println("Height[" + a + "] = " + heights[a]);
System.out.println("Sum = " + sum.setScale(2, BigDecimal.ROUND_HALF_UP));
}
return sum.divide(new BigDecimal(heights.length));
}
出力は次のとおりです。
Height[0] = 24
Sum = 0.00
Height[1] = 24
Sum = 0.00
Height[2] = 24
Sum = 0.00
Height[3] = 26
Sum = 0.00
Height[4] = 26
Sum = 0.00
Height[5] = 26
Sum = 0.00
単純なエラーだと思いますが、よろしくお願いします。