我已經編寫了一個編碼來接受來自用戶的noSem和noCourse。每個學期將有一個不同的課程。我的問題是,如果用戶輸入相同的noCourse值,我只能顯示數據。我希望它在學期間表現出不同。Java Array循環顯示不同的課程
這裏我的一些編碼:
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) {}
}
}
問題不談,你應該有一個名爲noCourse作爲CourseNo – Gaur93