你的位置:首页 > Java教程

[Java教程]用对象型泛型和ArraysList写一个输入学员信息并展示


题目:录入学员信息并保存,当录入学员的编号为0时结束,展示出学员信息

//student类

public class Student {   

       public int id;

       public String name;    

       public int age;   

       public Student() {

             super();

       }

       public Student(int id, String name, int age) {

             super();

             this.id = id;   

             this.name = name;  

             this.age = age;  

}

//text

public class Text {
      public static void main(String[] args) {
            System.out.println("*****录入学员信息,当录入学员的编号为0时结束*****");
            Scanner input = new Scanner(System.in);
            ArrayList<Student> arraylist = new ArrayList<Student>();
            //输入学员信息
            while(true){
                 Student student = new Student();
                 System.out.print("请输入学员学号:");
                 student.id = input.nextInt();
                 if(student.id==0){
                       System.out.println("结束录入!");
                       break;
                 }else{
                       System.out.print("请输入学员姓名:");
                       student.name = input.next();
                       System.out.print("请输入学员年龄:");
                       student.age = input.nextInt();
                       arraylist.add(student);
                 }
             }
        
             //输出学员信息
             System.out.println("学员的信息如下:");
             System.out.println("学号\t姓名\t年龄");
             Iterator<Student> iterator = arraylist.iterator();
             while(iterator.hasNext()){
                    Student a = iterator.next();
                    System.out.println(a.id+"\t"+a.name+"\t"+a.age);
              }

}

运行结果: