?public void testPut() {?? ??? ?// 創(chuàng)建一個(gè)Scanner對(duì)象,用于輸入學(xué)生的ID和姓名?? ??? ?Scanner console = new Scanner(System.in);?? ??? ?int i = 0;?? ??? ?while (i < 3) {?? ??? ??? ?System.out.println("請(qǐng)輸入學(xué)生的ID:");?? ??? ??? ?String ID = console.next(); ?? ??? ??? ?// 判斷ID是否被占用?? ??? ??? ?Student st = students.get(ID);???? 出現(xiàn)java.lang.NullPointerException?? ??? ???? if (st == null) {?? ??? ??? ??? ?System.out.println("請(qǐng)輸入學(xué)生的姓名:");?? ??? ??? ??? ?String studentName = console.next();?? ??? ??? ??? ?// 創(chuàng)建新學(xué)生對(duì)象?? ??? ??? ??? ?Student newStudent = new Student(ID,studentName);?? ??? ??? ??? ?// 通過調(diào)用Students的put方法,添加ID-學(xué)生映射?? ??? ??? ??? ?students.put(ID, newStudent);?? ??? ??? ??? ?System.out.println("成功添加學(xué)生:" + students.get(ID).name);?? ??? ??? ??? ?i++;?? ??? ??? ?} else {?? ??? ??? ??? ?System.out.println("該學(xué)生的ID已經(jīng)被占用!");?? ??? ??? ??? ?continue;?? ??? ??? ?}?? ??? ?}??? }
添加回答
舉報(bào)
0/150
提交
取消