我刚接触Java编码。我需要计算电路板上每列的平均块数。以下是计算板上所有列中的块总数,然后除以列数。
这是我到目前为止,但我认为这是不对的。我觉得我错过了什么。
public int getAverageColumnBlocks(Board board)
{
int avgColumnBlock = 0;
for (int col = 0; col < Board.WIDTH; col++)
{
for (int row = 0; row < Board.HEIGHT; row++)
{
if(board.isBlockAt(col, row))
{
avgColumnBlock++;
}
}
}
return avgColumnBlock;
}
public int getAverageColumnBlocks(Board board)
{
int total = 0;
for (int col = 0; col < Board.WIDTH; col++)
{
for (int row = 0; row < Board.HEIGHT; row++)
{
if(board.isBlockAt(col, row))
{
total++;
}
}
}
return total/Board.WIDTH;
}