2016-10-15 44 views
0

我已經編寫了一個編碼來接受來自用戶的noSem和noCourse。每個學期將有一個不同的課程。我的問題是,如果用戶輸入相同的noCourse值,我只能顯示數據。我希望它在學期間表現出不同。Java Array循環顯示不同的課程

這裏是一些輸出: I seems the loop is working, but the data is null because cant access the specific course between semester

這裏我的一些編碼:

class studentCourse { 

int noCourse, noSem; 

void course() { 

    BufferedReader inData = new BufferedReader(new InputStreamReader(System.in)); 

    try { 

     System.out.print("Enter no of semester : "); 
     noSem = Integer.parseInt(inData.readLine()); 

     String[][] sbjName = new String[noSem][]; 
     String[][] courseCode = new String[noSem][]; 
     int[][] CHour = new int[noSem][]; 
     int[][] Marks = new int[noSem][]; 

     // Semester loop 
     for(int i = 0; i < noSem; i++) { 

      System.out.println("\n\tSemester" + (i + 1)); 
      System.out.print("Enter number of course : "); 
      noCourse = Integer.parseInt(inData.readLine()); 

      sbjName[i] = new String[noCourse]; 
      courseCode[i] = new String[noCourse]; 
      CHour[i] = new int[noCourse]; 
      Marks[i] = new int[noCourse]; 

      // course details loop 
      for(int u = 0; u < noCourse; u++) { 

       System.out.print("Enter Course Code : "); 
       courseCode[i][u] = inData.readLine(); 

       System.out.print("Enter Course Name : "); 
       sbjName[i][u] = inData.readLine(); 

       System.out.print("Enter Credit Hour : "); 
       CHour[i][u] = Integer.parseInt(inData.readLine()); 

       System.out.print("Enter Marks : "); 
       Marks[i][u] = Integer.parseInt(inData.readLine()); 

       System.out.println("\n"); 
      } 
     } 
     for(int row = 0; row < noSem; row++) { 

     System.out.println("\nResult Semester " + (row + 1)); 
     System.out.println("Course Code\t Course Name\t Credit Hour\t Marks\n"); 
     courseCode[row] = new String[noCourse]; 

      for(int col = 0; col < noCourse; col++) { 

       // display course code 
       System.out.print("Value row = " + row); 
       System.out.print("Value of col = " + col); 
       System.out.print(courseCode[row][col] + "\t");      
       System.out.print("\n"); 


      } 
      /* 

       // display subject name 
       for(int x = 0; x < 1; x++) { 
        for(int y = 0; y < sbjName[x].length; y++) { 
         System.out.print(sbjName[x][y] + "\t"); 

        } 

       } 

       // display credit hour 
       for(int x = 0; x < CHour.length; x++) { 
        for(int y = 0; y < CHour[x].length; y++) { 
         System.out.print(CHour[x][y] + "\t"); 
        } 
       } 

       // display marks 
       for(int x = 0; x < Marks.length; x++) { 
        for(int y = 0; y < Marks[x].length; y++) { 
         System.out.print(Marks[x][y]); 
        } 
       } */ 
      } 
    } 
    catch (ArrayIndexOutOfBoundsException Aobj) { 
     System.out.println("Could not access the index!"); 
    } 

    catch (Exception Eobj) {} 
} 
} 
+0

問題不談,你應該有一個名爲noCourse作爲CourseNo – Gaur93

回答

0
  1. 您已經從它顯示的結果之前,重新初始化courseCode。刪除此行代碼

    courseCode [row] = new String [noCourse];

顯示您的結果之前,一切都會正常工作。

  1. 變量noCourse對於每個學期可能會有所不同。這意味着使用變量來獲得您的結果是容易出錯的。相反,使用任何數組的長度,例如courseCode獲取你的結果:
for(int row = 0; row < courseCode.length; row++) { 
    System.out.println("\nResult Semester " + (row + 1)); 
    System.out.println("Course Code\t Course Name\t Credit Hour\t Marks\n");  
    for(int col = 0; col < courseCode[row].length; col++) { 
     System.out.print(courseCode[row][col] + "\t"); 
     System.out.print(sbjName[row][col] + "\t"); 
     System.out.print(CHour[row][col] + "\t"); 
     System.out.print(Marks[row][col] + "\t");      
     System.out.print("\n"); 
    } 
} 
+0

我已經嘗試過這一點。如果用戶爲noCourse輸入相同的值,則只顯示輸出。我得到ArrayIndexOutOfBounds我使用.. –

+0

我測試了代碼,它刪除該行代碼後正常工作。 http://prntscr.com/cu6udy – Olantobi

+0

嘗試輸入不同的課程。例如,第1學期的課程= 2,第2學期的課程= 3。 –