2 次元配列を使用して、学生、姓名、4 学年に関する情報を格納するプログラムを作成しようとしています。
ユーザーは、そのすべてをプログラムに入力し、ボタンを押してコースの平均を計算できる必要があります。また、学生の姓名を入力して、平均成績を出力できるようにする必要があります。プログラムは、保存されているすべての情報を一覧表示できる必要があります。
おそらく配列リストを使用する方が簡単だと思いますが、これはコース用であり、2次元配列を使用する必要があることを指定しています。
使用している開発環境は NetBeans 6.5 です (6.8 はデザイン ウィンドウを保持する最後のバージョンです)。はい、これは非常に時代遅れであることは認識していますが、それを使用する必要があります。このバージョンにはデザイン ウィンドウがあるため、GUI コーディングについて心配する必要はありません。必要なラベルとテキスト領域とボタンをドラッグするだけです。最大 15 人の生徒を処理できる必要があります。コードの背後にある私の考えと理論を説明していきます。
//Create and intialize array to have 15 rows and 6 columns.
String [][] records = new String [15][6];
int n = 1;
double clicked = 0;
private void addActionPerformed(java.awt.event.ActionEvent evt) {
//The basic idea behind the add method here is that we take the input from the user and assign it to the array by taking the number of times this method has been used as a reference for where it should go in the array. This method actually works fine, I just put in here for reference in case someone thinks it might be important.
String nameFirst,nameLast, grade1,grade2, grade3, grade4;
nameFirst = firstName.getText();
nameLast = lastName.getText();
grade1 = class1Grade.getText();
grade2 = class2Grade.getText();
grade3 = class3Grade.getText();
grade4 = class4Grade.getText();
records[n-1][0] = nameFirst;
records[n-1][1] = nameLast;
records[n-1][2] = grade1;
records[n-1][3] = grade2;
records[n-1][4] = grade3;
records[n-1][5] = grade4;
output.append(records[n-1][0] + "\n");
output.append(records[n-1][1] + "\n");
output.append(records[n-1][2] + "\n");
output.append(records[n-1][3] + "\n");
output.append(records[n-1][4] + "\n");
output.append(records[n-1][5] + "\n");
n++;
firstName.setText("");
lastName.setText("");
class1Grade.setText("");
class2Grade.setText("");
class3Grade.setText("");
class4Grade.setText("");
clicked++;
}
private void listActionPerformed(java.awt.event.ActionEvent evt) {
//This is the code that we were shown to display all elements in the array but for whatever reason it doesn't want to go past row 0(first row) and I don't know why.
for (int row = 0; row <= 16; row++) {
output.append(row + "\n");
for( int col=0; col <= 6; col++) {
output.append(records[row][col] +"\n");
}
}
//This is the code I added in to make sure that the add method was actually putting things where they belonged. It is but it did show that the above loop wasn't working.
//output.append(records[0][0] + "\n");
//output.append(records[0][1] + "\n");
//output.append(records[0][2] + "\n");
//output.append(records[0][3] + "\n");
//output.append(records[0][4] + "\n");
//output.append(records[0][5] + "\n");
//output.append(records[1][0] + "\n");
//output.append(records[1][1] + "\n");
//output.append(records[1][2] + "\n");
//output.append(records[1][3] + "\n");
//output.append(records[1][4] + "\n");
//output.append(records[1][5] + "\n");
}
private void calculateCourseAverageActionPerformed(java.awt.event.ActionEvent evt) {
double total1 = 0, total2 = 0, total3 = 0, total4 =0;
double col2, col3, col4, col5;
//col2 = Double.parseDouble(records[0][2]);
//Here I try to add up all the stored values in a particular column. Originally it kept giving back a floating decimal error. It turned out the the value in the array was being multiplied by 64(4 from first loop, 16 from second, 16*4=64). Now its only being multiplied by 16, not much better but an improvement none the less.
for(int col = 2; col<=5; col++){
for(int row = 0; row<=15; row++){
if(col == 2){
col2 = Double.parseDouble(records[0][2]);
total1 = total1 + col2;
}
if(col == 3){
col3 = Double.parseDouble(records[0][3]);
total2 = total2 + col3;
}
if(col == 4){
col4 = Double.parseDouble(records[0][4]);
total3 = total3 + col4;
}
if(col == 5){
col5 = Double.parseDouble(records[0][5]);
total4 = total4 + col5;}
}
}
output.append("The average for course 1 is " + total1/4);
output.append("The average for course 2 is " + total2/4);
output.append("The average for course 3 is " + total3/4);
output.append("The average for course 4 is " + total4/4);
}
private void calculateStudentAverageActionPerformed(java.awt.event.ActionEvent evt) {
String nameFirst, nameLast;
double studentAverage, total = 0, grade, finalGrade;
boolean continuation;
int student = 0;
//here we take the first name and last name(that the user should input). The method is supposed to check through the array for the first name and if it finds it it should check the last name then add up all the number is that particular row. Tests show that it seems to work fine until the Name1 Name2 area(*), it shows that the records[row][0] and records[row][1] are equal to null which negates the rest of my code here.
nameFirst = firstName.getText();
nameLast = lastName.getText();
output.append("Name check " + nameFirst + nameLast + "\n");
while(continuation = true){
for(int row=0;row<=15;row++){
for(int col=0;col<=0;col++){
*
output.append("Name1 " + records[row][0] + "\n");
output.append("Name2 " + records[row][1] + "\n");
*
if(records[row][0].equals(nameFirst)){
if(records[row][1].equals(nameLast)){
continuation = false;
student = row;
String test = Integer.toString(student);
output.append("Student row # " + test + "\n");
}
}
}
}
}
for(int col = 2; col<=5; col++){
grade = Double.parseDouble(records[student][col]);
output.append("Grade " + grade + "\n");
total = total + grade;
output.append("Total " + total + "\n");
}
finalGrade = total / 4;
output.append(finalGrade + "\n");
output.append("First Name " + records[student][0] + "\n");
output.append("Last Name " + records[student][1] + "\n");
output.append(records[student][0] + records[student][1] + "'s average is " + finalGrade);
continuation = true;
}
ヘルプやアイデア、考えなど...非常に高く評価されます。私はこの 1 か月間、これと一致するゲームの割り当てに行き詰まっており、他の誰かに意見や提案を求める時が来たと考えました。