为什么输出的是key值?哪位大佬告诉我哈

来源:5-2 学生选课---使用 Map 添加学生

慕哥5034676

2018-01-15 22:26

public class MapTest {

public Map<String, Student> students;


public MapTest() {

this.students = new HashMap();

}


public void mapAdd() {

Scanner console = new Scanner(System.in);


int i = 0;

while (i < 3) {

System.out.println("请输入学生ID:");

String ID = console.next();

Student stu = students.get(ID);

if (stu == null) {

System.out.println("input student's name:");

String name = console.next();

Student newStudent = new Student(ID, name);

students.put(ID, newStudent);

System.out.println("成功添加:" + students.get(ID).name);

i++;

} else {

System.out.println("已经存在ID再输:");

continue;

}

}

testKeySet();

}


public void testKeySet() {

Set<String> keySet = students.keySet();

for (String stuId : keySet) {

Student stu = students.get(stuId);

if (stu != null) {

System.out.print(stu.name);

}


}

}


public static void main(String[] args) {

MapTest mt = new MapTest();

mt.mapAdd();

}

}

请输入学生ID:

1

input student's name:

tom

成功添加:1

请输入学生ID:

2

input student's name:

jay

成功添加:2

请输入学生ID:

3

input student's name:

jack

成功添加:3

123


写回答 关注

1回答

  • 慕哥5034676
    2018-01-15 22:53:51

    解决了~

    慕码人705...

    咋解决的倒是说啊

    2019-11-30 11:58:18

    共 1 条回复 >

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409768 学习 · 4463 问题

查看课程

相似问题