用對象型泛型和ArraysList寫一個輸入學員資訊並展示,arrayslist學員

來源:互聯網
上載者:User

用對象型泛型和ArraysList寫一個輸入學員資訊並展示,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);
              }

}

運行結果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.