0

では、これは生徒の名前とテストの点数をリストするプログラムになります。私はそれをすべて理解しましたが、1つの小さなことです。生徒の名前をリストする行が必要です...その後、まったく同じ名前を繰り返さずに別の生徒をリストに追加できます。(1 と 2 のラベルが付いた行を参照)

1 行目は、教師が入力する firstNameField と lastNameField から名前を収集します...そして 2 行目は、別の生徒が追加されたときに名前が繰り返されないように、配列の「1 つ上に移動」します。

残念ながら...別の生徒が追加されるたびに、このように少し見えます...

例:

ジェーン・ドゥヌル 65 76 45 89 ジェーン・ドゥヌル 65 87 45 76 ジョン・スミスヌル 65 76 45 89

問題を明確に見ることができます....「null」があり、さらに2番目の名前が繰り返されています(Jane Doe)

では、これを防ぐにはどうすればよいでしょうか。

public class StudentGradesView extends FrameView {

int [][] aryStudent = new int [15][4]; //number of student (15) test scores (4 scores)
String[][] aryNames = new String[15][2]; //lists the names of students
int numOfStudents = 0; //starts off students from zero...
int marks = 0; 

int test1;
int test2;
int test3;
int test4;

public StudentGradesView(SingleFrameApplication app) {

//GUI のもの...

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          

    String currentList = studentListField.getText();

    //Collects integers from test1Field though test4Field
    aryStudent[numOfStudents][0] = Integer.parseInt(test1Field.getText());
    aryStudent[numOfStudents][1] = Integer.parseInt(test2Field.getText());
    aryStudent[numOfStudents][2] = Integer.parseInt(test3Field.getText());
    aryStudent[numOfStudents][3] = Integer.parseInt(test4Field.getText());

    StringBuilder sb = new StringBuilder();


    for (int x=0; x <= numOfStudents && x<15; x++) {
        sb.append(firstNameField.getText()).append(" ").append(lastNameField.getText()); //(1)
        sb.append(aryNames[x][0]); //(2)
        for (int y=0; y < 4; y++) {
            sb.append(" ").append(aryStudent[x][y]);
            studentListField.setText(sb.toString() + "\n" + currentList);
        }
        sb.append("\n");

    }
    numOfStudents ++;
}  

追加: したがって、「sb.append(aryNames[x][0]); //(2)」は重要ではなく、それがなければ、次のように出力されることを除いて名前は繰り返されません...例

メアリー・ジョーンズ 56 76 56 87 ジョン・スミス 45 45 45 45 ジョン・スミス 45 45 45 45

したがって、追加された 2 番目の名前が 2 回繰り返されます。

4

1 に答える 1

0

データを aryNames のどこに入れていますか?

あなたはそれを初期化します:

String[][] aryNames = new String[15][2]; //lists the names of students

そして、あなたはそれを使用します:

sb.append(aryNames[x][0]); //(2)

しかし、それはどこに住んでいましたか?参照するのはこれだけで、名前を追加した直後です。だからあなたはnullになっています

からです:

sb.append(firstNameField.getText()).append(" ").append(lastNameField.getText()); //(1)

ただし、aryNamesデータが挿入されていない場合はaryNames[x][0]null になるため、null が追加されます。

aryNamesしたがって、配列のどこにデータを入力しているかを確認します。

于 2013-04-29T18:15:09.637 に答える