A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 徐文豪 中级黑马   /  2015-5-13 22:44  /  617 人查看  /  14 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

我有5个学生,请把这个5个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。
学生:Student
成员变量:name,age
构造方法:无参,带参
成员方法:getXxx()/setXxx()
               

这个的思路是什么啊?一点头绪都没有。求解。

14 个回复

倒序浏览
创建5个Student对象
Student []stu={Student,Student,Student,Student,Student};
{:2_42:}我就是这样搞的
回复 使用道具 举报
这样可以?
回复 使用道具 举报
不会吧。。。
回复 使用道具 举报
顶一个!
回复 使用道具 举报
public class Demo {
        public static void main(String[] args) {
                //创建学生对象并传递参数
                Student s1=new Student("张三",15);
                Student s2=new Student("李四",12);
                Student s3=new Student("王五",17);
                Student s4=new Student("赵六",13);
                //创建一个保存学生名字的数组
                String []a=new String[4];
                //将学生中的数据添加进数组中
                a[0]=s1.getName();
                a[1]=s2.getName();
                a[2]=s3.getName();
                a[3]=s4.getName();
                //创建一个保存学生年龄的数组
                int []b=new int[4];
                //将学生年龄添加进数组中
                b[0]=s1.getAge();
                b[1]=s2.getAge();
                b[2]=s3.getAge();
                b[3]=s4.getAge();
                //遍历
                for (int i = 0; i < b.length; i++) {
                        System.out.println(a[i]+b[i]);
                }
        }
}

// 封装一个Student类
class Student {
        // 定义学生的姓名年龄
        private String name;
        private int age;

        // 无参构造
        public Student() {
                super();
                // TODO Auto-generated constructor stub
        }

        // 有参构造
        public Student(String name, int age) {
                super();
                this.name = name;
                this.age = age;
        }

        // get set方法
        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public int getAge() {
                return age;
        }

        public void setAge(int age) {
                this.age = age;
        }

}
回复 使用道具 举报 1 0
2楼是对的
回复 使用道具 举报
正在学习中
回复 使用道具 举报
rolan 中级黑马 2015-5-14 08:56:51
9#
楼上的答案很好,
回复 使用道具 举报
_hy 中级黑马 2015-5-14 09:49:04
10#
class Demo
{
        public static void main(String[] args)
        {       
                Student stu1=new Student("张三",20);//依据Student类的构造函数创建学生对象
                Student stu2=new Student("李四",21);
                Student stu3=new Student("王五",23);
                Student stu4=new Student("赵六",19);
                Student stu5=new Student("孙七",22);
                Student [] stu={stu1,stu2,stu3,stu4,stu5};//创建一个含学生姓名、年龄的数组
                System.out.println("学生信息"+":");
                for(int n=0;n<stu.length;n++)
                {
                        System.out.println(stu[n].getname()+","+stu[n].getage());}       
                }
}
class Student
{
        private String name;
        private int age;
        Student(String name,int age)//构造函数给对象初始化
        {       
                this.name=name;
                this.age=age;
        }
        public String getname()//获取姓名
        {
                return name;
        }
        public int getage()//获取年龄
        {
                return age;
        }

}
回复 使用道具 举报
谢谢了
回复 使用道具 举报
这个思路,你不是已经写得比较清楚了么,学生类Student有了,成员变量name,age有,构造方法用于初始化学生信息有了,类中的方法getxxx用于读取学生的信息也有,这个不就是思路么?
回复 使用道具 举报
并不清楚
回复 使用道具 举报
属于哪个章节的内容,没思路
回复 使用道具 举报
学习学习
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马