问答详情
源自:5-2 学生选课---使用 Map 添加学生

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

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


提问者:慕哥5034676 2018-01-15 22:26

个回答

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

    解决了~