1

私は自分のコードを見たり見たりしてきましたが、結果が常にhighestGrade値であり、配列内のすべての位置で異なる値ではない理由をまだ理解できません。

これが私のコードです:

int[] gradesDescription;
int[] gradesCount;

gradesDescription = new int[(highestGrade-lowestGrade) + 1];
gradesCount = new int[(highestGrade-lowestGrade) + 1];

for(int b = lowestGrade; b <= highestGrade; b++){
  Arrays.fill(gradesDescription, b);
}

for(int d = 0; d < gradesDescription.length; d++){
 System.out.println("Grade: " + gradesDescription[d] + 
                    " had " + gradesCount[d] + " students with the same grade.");

私が見逃している論理は何ですか; 私がやろうとしていることを達成するためのより良い方法はありますか?

本当にありがとう!

4

3 に答える 3

2
for(int b = lowestGrade; b <= highestGrade; b++){
     Arrays.fill(gradesDescription, b);
}

この行は、配列b内のすべての位置に値を配置しますgradesDescription。したがって、毎回同じ値です。

于 2013-05-01T21:12:44.647 に答える
2

この行が問題の原因です:

Arrays.fill(gradesDescription, b);

これにより、すべての値が に割り当てgradesDescriptionられbます。代わりに欲しいのは次のようなものです:

for(int b = 0; b < gradesDescription.length; b++) {
    gradesDescription[b] = b + lowestGrade;
}

ただし、このコードでさえ間違っているように見えます。成績が 70 点、80 点、100 点の生徒が 3 人いる場合、予想される動作は何ですか? gradesDescription.length最終的には 30 になりますが、実際には 3 だけでよいのでしょうか? gradesCountの要素が割り当てられているコードを省略したと思いますか?

于 2013-05-01T21:12:52.643 に答える