Java Array Loop显示不同的noCourse

时间:2016-10-15 09:21:47

标签: java arrays for-loop multidimensional-array

我已经编写了一个代码来接受来自用户的noSem和noCourse。每个学期都会有一个不同的noCourse。我的问题是,如果用户输入相同的noCourse值,我只能显示数据。我希望它能在学期之间展现出不同的东西。

这里有一些输出: 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) {}
}
}

1 个答案:

答案 0 :(得分:0)

  1. 在显示结果之前,您已重新初始化courseCode。删除这行代码

    courseCode [row] = new String [noCourse];

  2. 在显示结果之前,一切都会正常工作。

    1. 每个学期的变量noCourse可能会有所不同。这意味着使用变量来获得结果容易出错。相反,使用任何数组的长度,例如courseCode获取结果:
    2. 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");
        }
      }