2012-11-22 40 views
1

我試圖創建一個程序,使用二維數組存儲有關學生的信息,他們的名字和姓氏,然後4級。二維字符串Arrayb學生記錄程序

用戶必須能夠在程序中輸入所有內容並按下按鈕來計算課程的平均值。他們還應該能夠輸入學生的名字和姓氏,並輸出他們的平均成績。程序還應該能夠列出存儲在其中的所有信息。

我意識到它可能會更容易使用數組列表,但這是一門課程,它指定我必須使用二維數組。

我們使用的開發環境是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; 
} 

任何幫助或想法,想法等...將非常感激。過去一個月,我一直堅持這一點和一個匹配的遊戲任務,我認爲這是我要求別人提供意見和建議的時間。

+0

一兩件事,在你的'calculateCourseAverage ......'方法你從來沒有使用'行「變量。我猜你沒有按照你的意圖迭代數據,因爲這樣?你最需要解決的問題是什麼? – Grambot

+0

哦,對不起,我沒有將行變量更改爲0,因爲當我將行放入時,我得到一個浮點十進制錯誤,循環使值太大,以至於double類型不夠精確。我忘了將它從0改回(當我試圖找出錯誤時,我正在使用它進行調試)。 –

回答

0

你必須把15改爲4總...

output.append("The average for course 1 is " + total1/15); 
output.append("The average for course 2 is " + total2/15); 
output.append("The average for course 3 is " + total3/15); 
output.append("The average for course 4 is " + total4/15); 

行之後的是同樣的方法irerating 16倍,而不是15,這將拋出空指針異常,你的情況...更改

for(int row = 0; row<=15; row++){ 

for(int row = 0; row<15; row++){ 

下面的代碼將完全正常工作適合你...

double[] totals = new double[4]; 
     for(int row = 0; row<=15; row++){ 
      for(int col = 2; col<=5; col++){ 
       total[col - 2] = total[col - 2] + records[row][col]; 
      } 
     } 
    } 
0
for (int row = 0; row <= 16; row++) { 

所以當row = 16可能遇到的這條線:

output.append(records[row][col] +"\n"); 

可能拋出NullPointerException爲指標1516不會在那裏(指數將是0 - 14,你需要15行)根據您的聲明。

同樣是這條線的情況下for (int col = 0; col <= 6; col++) {

所以儘量將其更改爲這樣:

for (int row = 0; row < 15; row++) { 
     output.append(row + "\n"); 
     for(int col=0; col < 6; col++) { 
      output.append(records[row][col] +"\n"); 
     } 
} 
+0

謝謝。這解決了我的一個問題。它給出了一些空值,但重要的是它一直通過。我可以處理null。非常感謝。 –