重寫了equal后name還是提示false 前一節的課程也是一樣 怎么回事
??? public void testContainsKeyOrValue(){
?? ??? ?System.out.println("請輸入要查詢的學生ID:");
?? ??? ?Scanner console=new Scanner(System.in);
?? ??? ?String id=console.next();
?? ??? ?System.out.println("您輸入的學生ID為:" + id + ",在學生表中是否存在:"
?? ??? ??? ??? ?+ students.containsKey(id));
?? ??? ?if (students.containsKey(id)) {
?? ??? ??? ?System.out.println("對應的學生為:" + students.get(id).name);
?? ??? ?}
?? ??? ?System.out.println("請輸入要查詢的學生姓名:");
?? ??? ?String name=console.next();
?? ??? ?System.out.println("您輸入的學生ID為:" + name + ",在學生表中是否存在:"
?? ??? ??? ??? ?+ students.containsKey(name));
?? ??? ?if (students.containsKey(new Student(null,name))) {
?? ??? ??? ?System.out.println("對應的學生為:" + name);
?? ??? ?}
?? ?}
請輸入學生ID:
1
請輸入學生姓名:
小黑
成功添加學生:小黑
請輸入學生ID:
2
請輸入學生姓名:
小紅
成功添加學生:小紅
請輸入學生ID:
3
請輸入學生姓名:
小號
成功添加學生:小號
總共有:3個學生!
學生:小號
學生:小紅
學生:小黑
請輸入要查詢的學生ID:
2
您輸入的學生ID為:2,在學生表中是否存在:true
對應的學生為:小紅
請輸入要查詢的學生姓名:
小黑
您輸入的學生ID為:小黑,在學生表中是否存在:false
2016-06-30
?System.out.println("請輸入要查詢的學生姓名:");
?? ??? ?String name=console.next();
?? ??? ?System.out.println("您輸入的學生ID為:" + name + ",在學生表中是否存在:"
?? ??? ??? ??? ?+ students.containsKey(name)); ??
?? ??? ?if (students.containsKey(new Student(null,name))) {
?? ??? ??? ?System.out.println("對應的學生為:" + name);
?? ??? ?}
上面斜體加粗的地方錯了
應該是students.containsValue(new Student(null,name))
2016-06-30
好吧? 我說為啥每回判定學生姓名都是false 原來是我復制的時候忘記修改了.....