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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 小泥人 中级黑马   /  2016-10-1 15:07  /  617 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

这是我无意中在论坛上看见的一个问题 ,现将代码贴出,请多多指教
/*(1)定义一个学生类Student,属性:姓名(String name)、班级班号(String no)、成绩(double score)
   (2)将若干Student对象存入List集合,并统计每个班级的总分和平均分*/
/*
* 总体思路:
* 1、先通过键盘录入将学生存进  ArrayList集合
* 2、通过遍历集合 得到班级数
* 3、将班级作为键,班级的总分数作为值  存进Map集合
* 4、根据班级数定义一个数组,数组的元素用来记录班级人数
* 5、将班级人数和总分数(Map集合的值)通过计算得到班级平均分数
* */

定义学生类
public class Student {
        private String name;
        private String num;
        private double score;
        Student(){}
        Student(String name,String num,double score){
                this.name=name;
                this.num=num;
                this.score=score;
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public String getNum() {
                return num;
        }
        public void setNum(String num) {
                this.num = num;
        }
        public double getScore() {
                return score;
        }
        public void setScore(double score) {
                this.score = score;
        }
        @Override
        public String toString() {                                                //重新定义 toString() 方法
                return  name.toString() + "," + num.toString() + ", " + Double.toString(score);
        }
}

主方法
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;

public class StudentDemo {
       

        public static void main(String[] args) {
                ArrayList<Student> al=new ArrayList<Student>();
                inputStudent(al);
                averageSum(al,ergoBanJi(al));
               
        }
        //通过键盘录入的方式存入学生数据,也可以利用IO通过读取文件存入数据,以空格或","对数据进行切割
public static void inputStudent(ArrayList<Student> al){
        Scanner sc=new Scanner(System.in );
        while (true){
                System.out.println("请{输入学生姓名:");       
                String s1=sc.nextLine();
                if(s1.equals("over")){                                                                                                        //录入 over 表示结束
                        sc.close();
                        break;
                }else{
                System.out.println("请输入学生班级:");
                String s2=sc.nextLine();
                System.out.println("请输入学生分数");
                String s3=sc.nextLine();                                                                                                                //分数以字符串接收  
                al.add(new Student(s1,s2,Double.parseDouble(s3)));                                //分数 先以字符串接收,再强转成double 类型
                }
        }
}
public static int ergoBanJi(ArrayList<Student>  al){
        int max=Integer.parseInt(al.get(0).getNum());
                for(int i=0;i<al.size();i++){
                        if(Integer.parseInt(al.get(i).getNum())>max){
                                max=Integer.parseInt(al.get(i).getNum());                                //获取最大班级数,班级一般是连续的,最大班级数就是总的班级数
                        }
                }
                return max;               
        }
public static void averageSum(ArrayList<Student> al,int n){                                        //将ergoBanji的结果  max 做为参数传递给函数
        int[] arr=new int[n];                                                                                                                                        //以班级数n做为数组的长度
        TreeMap<String,Double> tm=new TreeMap<String,Double>();
        for(Student ss:al){
                if(tm.containsKey(ss.getNum())){
                                        tm.put(ss.getNum(), tm.get(ss.getNum())+ss.getScore());
                                        arr[Integer.parseInt(ss.getNum())-1]++;                                                //数组元素记录班级人数,数组元素默认初始化值为0,所以班级人数是对应数组元素+1
                                                                        //因数组元素从0开始,arr[0]记录第一个班级的人数。arr第i个元素记录第i+1个班级的人数
                }else{
                        tm.put(ss.getNum(), ss.getScore());
                }
        }
        Set<String> s=tm.keySet();
        for(String ss:s){
                double d=tm.get(ss)/(arr[Integer.parseInt(ss)-1]+1);                                //获取平均分
                System.out.println("第"+ss+"班的平均分是:"+d);                                                //将平均分显示在控制台,也可以将平均分写进文件
        }
}
}



2 个回复

倒序浏览
顶顶顶~
回复 使用道具 举报
顶顶顶~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马