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

chenjiasen

中级黑马

  • 黑马币:18

  • 帖子:31

  • 精华:0

(综合)有如下Student 对象
               其中,classNum 表示学生的班号,例如“class05”。
               有如下List
               List list = new ArrayList();
               list.add(new Student(“Tom”, 18, 100, “class05”));
               list.add(new Student(“Jerry”, 22, 70, “class04”));
               list.add(new Student(“Owen”, 25, 90, “class05”));
               list.add(new Student(“Jim”, 30,80 , “class05”));
               list.add(new Student(“Steve”, 28, 66, “class06”));
               list.add(new Student(“Kevin”, 24, 100, “class04”));
               在这个list 的基础上,完成下列要求:
               1) 计算所有学生的平均年龄
               2) 计算各个班级的平均分

15 个回复

倒序浏览
有没有大神来帮我解答啊!
回复 使用道具 举报
在Student类中定义其中要求的变量外,另加平均分和平均年龄的变量
重写tostring方法和get方法和构造方法
在有参构造参数列表中定义要求的变量
get方法只需要获取到平均年龄和平均分
tostring方法必须重写所有变量
定义一个遍历的方法
添加集合中的元素
调用遍历方法即可
回复 使用道具 举报
涨姿势了!!6666
回复 使用道具 举报

大兄弟 技术分怎么拿
回复 使用道具 举报
用list<Student> 和 Map<String,classroom>要好做一点
回复 使用道具 举报
本帖最后由 彼岸轮回 于 2016-6-19 23:45 编辑

                ArrayList<Student> al = new ArrayList<Student>();                                                      al.add(new Student("张三","class1",68,23));                 
                        al.add(new Student("小明","class2",88,24));              
                        al.add(new Student("李四","class1",99,25));                 
                        al.add(new Student("小红","class2",85,18));            
                        al.add(new Student("王五","class1",66,20));               
                        al.add(new Student("狗蛋","class2",79,26));              
                        al.add(new Student("赵六","class1",88,26));            
                        al.add(new Student("日天","class3",60,1));               
                        al.add(new Student("日地","class3",90,1));               
                        al.add(new Student("日空气","class3",66,1));                       
                        int sumage = 0;                       
                        Map<String, Integer> m1 = new HashMap<>();              
                        Map<String, Integer> m2 = new HashMap<>();            
                        for(Student s : al){                        
                                 int age = s.getAge();                       
                                 m1.put(s.getNo(),m1.containsKey(s.getNo())?m1.get(s.getNo())+1 : 1);                        
                                 m2.put(s.getNo(),m2.containsKey(s.getNo())?m2.get(s.getNo())+s.getScore() : s.getScore());                       
                                 sumage += age;  
               }               
                        System.out.println("所有学生的平均年龄为:"+sumage/al.size());            
                        for(String s : m1.keySet()){                  
                                         System.out.println(s + "....总分为:" + m2.get(s) + "....平均分为:" + m2.get(s)/m1.get(s));      
              }

回复 使用道具 举报
楼主可以看下借鉴下
回复 使用道具 举报
dubei1993 来自手机 中级黑马 2016-6-19 23:58:09
9#
创建学生资源的年龄,分数的get方法。增强for遍历,定义初始变量,做累加运算。over
回复 使用道具 举报
彼岸轮回 发表于 2016-6-19 23:44
楼主可以看下借鉴下

大神请问家住何处
回复 使用道具 举报
chenjiasen 发表于 2016-6-20 23:40
大神请问家住何处

额,楼主我不约.........
回复 使用道具 举报
强势围观
回复 使用道具 举报
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

public class List {

        public static void main(String[] args) {

                // 拿到已知条件,且已建好Stdent类
                // Student类中定义姓名(name)、年龄(age)、分数(score)、班级名(classNum)、定义有参构造、重写equals方法
                List<Student> list = new ArrayList<Student>();

                list.add(new Student("Tom", 18, 100, "class05"));
                list.add(new Student("Jerry", 22, 70, "class04"));
                list.add(new Student("Owen", 25, 90, "class05"));
                list.add(new Student("Jim", 30, 80, "class05"));
                list.add(new Student("Steve", 28, 66, "class06"));
                list.add(new Student("Kevin", 24, 100, "class04"));

                //定义一个变量统计年龄总和
                int sumAge = 0;

                //定义一个hashMap用来确保每个班级只输出一次
                HashMap<String, Object> map = new HashMap<String, Object>();

                //遍历list集合
                for (Student s : list) {

                        //计算所有人的年龄总和
                        sumAge += s.getAge();

                        //定义字符串来表示一个班级
                        String str = s.getClassNum();

                        //接受该班级的总分数
                        int sumScore = 0;

                        //该班级人数
                        int cound = 0;

                        for (Student s1 : list) {

                                //list集合中若有该班级的人,分数相加,人数加一
                                if (s1.getClassNum().equals(str)) {

                                        sumScore += s1.getScore();
                                        cound++;
                                }
                        }

                        //求的一个班级的平均分和认输,添加进map集合
                        double aveScore = sumScore * 1.0 / cound;
                        map.put(s.getClassNum() , aveScore);

                }

                //获得所有班级名的集合
                Set<String> keySet = map.keySet();

                //打印该班级的班级名、平均分
                for (String key : keySet) {
                        System.out.println("班级" + key + "的平均分为:" + map.get(key));
                }

                //打印所有学生平均年龄
                System.out.println("所有学生的平均年龄为:" + (sumAge * 1.0 / list.size()));
        }

}

楼主可以参考下,若有不对的地方,请指证~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马