2 回答

TA貢獻1799條經(jīng)驗 獲得超8個贊
有 2 點需要修復:處理數(shù)據(jù) (ENG-105-3-A) 和緩沖區(qū)。
String[][] courses = new String[length][4];
System.out.println("Follow this model when entering your courses: ENG-105-3-A");
for(int counter = 0; counter < length; counter++){
System.out.println("Course "+(counter+1));
//Solution
courses[counter] = input.next().split("-"); //data are separated by "-"
input.nextLine(); //Cleanning buffer
}

TA貢獻1836條經(jīng)驗 獲得超13個贊
要實現(xiàn)您想要的內(nèi)容,可以像這樣工作:
String[][] courses = new String[length][];
System.out.println("Follow this model when entering your courses: ENG 105 3 A");
for (int counter = 0; counter < length; counter++){
System.out.println("Course "+(counter+1));
courses[counter] = input.nextLine().split("\\s+");
}
由于這是拆分課程,因此它會生成一個數(shù)組數(shù)組,如下所示:
[["ENG","105","A","3"], ["MAT", "102", "A", "4"]]
另一方面,如果要在用戶輸入關(guān)鍵字時停止輸入,則需要如下所示的內(nèi)容:
List<String[]> courses = new ArrayList<String[]>;
System.out.println("Follow this model when entering your courses: ENG 105 3 A");
String course = input.next();
while (!course.equals("end")){
courses.add(course.split("\\s+"));
String course = input.nextLine();
}
添加回答
舉報