2016-10-31 86 views
1

我必須創建一個使用二維數組來存儲學生姓名及其成績的程序。用戶輸入學生姓名和他們的成績,然後應該添加。它必須能夠保存15名學生的數據。有人能告訴我如何將數據添加到這樣的數組?我已經附加了我的代碼,以及我的設計預覽。如何將數據添加到二維數組?

public class StudentGrades extends javax.swing.JFrame { 
    double [][] database = new double[4][15]; 
/** 
* Creates new form StudentGrades 
*/ 
public StudentGrades() { 
    initComponents(); 
    displayButton.setEnabled(false); 
    studentButton.setEnabled(false); 
    courseButton.setEnabled(false); 
} 

...

private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {           
    //set code to close program 
    System.exit(0); 
}           

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {           
    //clear output area 
    outputArea.setText(null); 

    //enable buttons 
    displayButton.setEnabled(true); 
    studentButton.setEnabled(true); 
    courseButton.setEnabled(true); 

    //declare variables 
    double grade1 = Double.parseDouble(test1.getText()); 
    double grade2 = Double.parseDouble(test2.getText()); 
    double grade3 = Double.parseDouble(test3.getText()); 
    double grade4 = Double.parseDouble(test4.getText()); 

User Interface

+0

是否真的必須是一個數組?你不能在同一個數組中保存不同的數據類型 – maszter

回答

0

任何另外的數據爲二維陣列是基於行和列:

裝置,用於2D陣列大小爲NXN:

database[0];  // Related to the first row in the array 
database[0][0]; // Is the top left cell in the array. 
database[N-1]  // Related to the last array row 
database[N-1][N-1] // Is the down right cell in the array. 

如果你想添加grade3在數組中的第二個學生,你應該插入到等級:

database[1][2] 

爲什麼? 由於陣列中的計數是基於零,因此,第二個學生數據被保存在

database[1] // The second array row. 

第一級應保存在列號0:

database[1][0] 

第二級應該是保存在列號1:

database[1][1] 

第三等級應保存在列號2:

database[1][2] 

在這裏,你有一個非常有用的link related to grades 2d-arrays

提示最佳實踐:

當使用二維數組這是一個很好的做法,使數字陣列與意義靜態最終名稱英寸相反的:

double [][] database = new double[4][15]; 

例如,你可以試試下面的語法:

private static final Integer NUM_OF_STUDENTS = 4 
private static final Integer NUM_OF_TESTS = 15 
double [][] database = new double[NUM_OF_STUDENTS][NUM_OF_TESTS]; 
0

這是一個二維數組的樣子:

因此,根據我的理解,這是你應該如何聲明你的二維數組:

double [][] database = new double[15][5]; 

這意味着有15行(15名學生)和5列(1列存儲名稱,另4列存儲該學生的成績)。現在您需要一種方法將名稱和等級存儲在同一個陣列中。所以唯一可以做到這一點的是如果你聲明數組的類型爲Object

Object [][] database = new Object[15][5]; 

一個對象可以容納任何類型的數據。這裏是一個例子:

database[0][0] = "jon skeet"; 
database[0][1] = 4.0; // grade in double type... you can also keep it as String 
database[0][2] = 4.0; 
database[0][3] = 4.0; 
database[0][4] = 4.0; 

如果二維數組不是必需的,那麼我會建議你使用一個HashMap或其他東西。

0

創建該類中的類學生把注意的數組,然後在StudenGrade類學生創造一個數組

class Student { 
int []grades; 
String name; 

public Student(String name, int NGrades){ 
this.name = name 
grades = new int[NGrades]; 
} 
public void setGrade(int index, int grade){ 
grade[index] = grade; 
} 
public int getGrade(int index){ 
return grade[index]; 
} 
public String getName(){ 
return name; 
} 
} 

class StudentGrades { 
Student students[]; 

public StudentGrades(int NStudents, int NGrades){ 
students = new Student[NStudents]; 
String names[] = {"Bob","Pete","Rafi","Roff"}; 
for(int i=0;i<NStudents;i++){ 
    students[i] = new Studen(names[i],NGrades); 
} 
}