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

请大家帮忙看看,是哪里错了?

package student;


import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

import java.util.Set;


public class Maptest {

public Map<String,STUDENT> Students;

public Maptest(){

this.Students=new HashMap<String,STUDENT>();

}

public void Testput(){

Scanner console=new Scanner(System.in);

int i=0;

while(i<3){

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

int ID=console.nextInt();

STUDENT ST=Students.get(ID);

if(ST==null){

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

String name=console.next();

STUDENT newStudent=new STUDENT(ID,name);

Students.put("ID", newStudent);

System.out.println("成功添加学生:"+Students.get(ID).getName());

i++;

}

else{

System.out.println("该ID已被占用!");

continue;

}

}

}

public void Testkeyset(){

Set<String> Keyset=Students.keySet();

System.out.println("总共有:"+Keyset.size());

for(String stuID:Keyset){

STUDENT st=Students.get(stuID);

if(st!=null){

System.out.println("学生:"+st.getName());

}

}

}


public static void main(String[] args) {

// TODO Auto-generated method stub

Maptest mp=new Maptest();

mp.Testput();

mp.Testkeyset();


}


}


提问者:慕先生4962489 2016-02-05 10:21

个回答

  • 司马长卿
    2016-02-19 13:16:52

    // 创建新的学生对象

    Student newStudent = new Student(ID, name);

    // 通过调用students的put方法,添加ID-学生映射

    students.put(ID, newStudent);

    为什么新建的学生对象里面已经有了ID和name 两个参数了。在put方法里面还要添加ID这个属性?

  • hainishishui
    2016-02-09 20:41:00

    个人感觉得你的问不是很大,你仔细看看 其实你在没有添加对象的时候就开始从student的对象中取元素了!你再看看TestPut方法中这句STUDENT ST=Students.get(ID)的位置!

  • 慕先生4962489
    2016-02-05 11:38:21

    package student;


    import java.util.HashMap;

    import java.util.Map;

    import java.util.Scanner;

    import java.util.Set;


    public class Maptest {

    public Map<String,STUDENT> Students;

    public Maptest(){

    this.Students=new HashMap<String,STUDENT>();

    }

    public void Testput(){

    Scanner console=new Scanner(System.in);

    int i=0;

    while(i<3){

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

    String ID=console.next();

    STUDENT ST=Students.get(ID);

    if(ST==null){

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

    String name=console.next();

    STUDENT newStudent=new STUDENT(ID,name);

    Students.put(ID, newStudent);

    System.out.println("成功添加学生:"+Students.get(ID).getName());

    i++;

    }

    else{

    System.out.println("该ID已被占用!");

    continue;

    }

    }

    }

    public void Testkeyset(){

    Set<String> Keyset=Students.keySet();

    System.out.println("总共有:"+Keyset.size());

    for(String stuID:Keyset){

    STUDENT st=Students.get(stuID);

    if(st!=null){

    System.out.println("学生:"+st.getName());

    }

    }

    }


    public static void main(String[] args) {

    Maptest mp=new Maptest();

    mp.Testput();

    mp.Testkeyset();


    }


    }


  • 慕先生4962489
    2016-02-05 11:36:37

    package student;


    import java.util.HashSet;

    import java.util.Set;


    public class STUDENT {

    private String id;

    private String name;

    public static Set<CLASS> Class;

    public String getId(){

    return id;

    }

    public void setId(String id){

    this.id=id;

    }

    public String getName(){

    return name;

    }

    public void setName(String name){

    this.name=name;

    }

    public STUDENT(String id,String name){

    this.id=id;

    this.name=name;

    this.Class=new HashSet<CLASS>();

    }


    }

    STUDENT类

  • 慕先生4962489
    2016-02-05 11:35:24

    解决了,,在STUDENT类里的Class加了一个static就解决了,,虽然不知为什么得加static

  • 慕先生4962489
    2016-02-05 10:24:06

    System.out.println("成功添加学生:"+Students.get(ID).getName());

  • 慕先生4962489
    2016-02-05 10:22:32

    请输入学生ID:

    1

    请输入学生姓名:

    小明

    Exception in thread "main" java.lang.NullPointerException

    at student.Maptest.Testput(Maptest.java:25)

    at student.Maptest.main(Maptest.java:49)