新手请教高手 用泛型和for循环打印输出
创建实体类Student包含学生信息(姓名,年龄,学习阶段),再创建一个测试类TestMain,定义一个Map集合对象,用于存储学生信息,Map的键是学号,将该学号对应的学生信息保存到Map中,并向控制台输出该Map的学员信息(使用泛型和增强for循环)。我自己写的代码:
public class user
{
private String name;
private String sex;
private int age;
private String pwd;
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setSex(String sex)
{
this.sex=sex;
}
public String getSex()
{
return sex;
}
public void setAge(int age)
{
this.age=age;
}
public int getAge()
{
return age;
}
public void setPwd(String pwd)
{
this.pwd=pwd;
}
public String getPwd()
{
return pwd;
}
}
import
import
import
import java.util.HashMap;
import java.util.Scanner;
public class class2 extends student
{
private int xuehao;
public void setXuehao(int xuehao)
{
this.xuehao=xuehao;
}
public int getXuehao()
{
return xuehao;
}
public static void main(String []args)
{
HashMap map=new HashMap();
class2 cl=new class2();
student st=new student();
Scanner sc=new Scanner(System.in);
String sno=null;
BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));
for( int i=0;i<3;i++)
{
System.out.println("**********【请录入第"+"【"+(i+1)+"】"+"个学生信息】*************");
System.out.println("【请输入学号】:");
try {
sno=strin.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("【请输入姓名】:");
st.setName(sc.next());
System.out.println("【请输入年龄】:");
st.setAge(sc.nextInt());
System.out.println("【请输入学期阶段】:");
st.setXuxi(sc.next());
}
System.out.println(map.get(sno));
System.out.println(map.get(st.getName()));
System.out.println(map.get(st.getAge()));
System.out.println(map.get(st.getXuexi()));
}
}