[Java] 请把这5个学生的信息存到数组中,并遍历数组,获得每个学生的信息 →→→→→进入此内容的聊天室

来自 , 2019-06-17, 写在 Java, 查看 108 次.
URL http://www.code666.cn/view/729c6888
  1. package cn.itcast_01;
  2. /*
  3.  * 有五个学生,请把这5个学生的信息存到数组中,并遍历数组,获得每个学生的信息。
  4.  *  学生:Student
  5.  *  成员变量:name,age
  6.  *  构造方法:无参,带参
  7.  *  成员方法:getXxx()/setXxx()
  8.  *  
  9.  *  分析:
  10.  *    A:创建学生类
  11.  *    B:创建学生数组
  12.  *    C:创建5个学生对象,并赋值
  13.  *    D:把五个学生对象放到数组中,
  14.  *    E:遍历学生数组。
  15.  *
  16.  */
  17.  
  18. public class ObjectArrayDemo {
  19.         public static void main(String[] args) {
  20.                 //创建数组
  21.                 Student [] arr = new Student[5];
  22.                
  23.                 //创建五个学生对象
  24.                 Student s1 = new Student("林青霞",28);
  25.                 Student s2 = new Student("风清扬",29);
  26.                 Student s3 = new Student("赵雅芝",30);
  27.                 Student s4 = new Student("孙允珠",25);
  28.                 Student s5 = new Student("黄婷婷",24);
  29.                
  30.                 //将对象赋给数组
  31.                 arr[0] = s1;
  32.                 arr[1] = s2;
  33.                 arr[2] = s3;
  34.                 arr[3] = s4;
  35.                 arr[4] = s5;
  36.                
  37.                 for(int i=0;i<5;i++){  
  38.                         //System.out.println(arr[i]);
  39.                         System.out.println(arr[i].getName()+","+arr[i].getAge());
  40.                 }
  41.                
  42.                
  43.         }
  44.  
  45. }
  46.  

回复 "请把这5个学生的信息存到数组中,并遍历数组,获得每个学生的信息"

这儿你可以回复上面这条便签

captcha