两个文件:info.txt 存放学生基本信息

  学号 学院 专业 姓名
  1001 计算机学院 软件工程 刘月
  1002 生物工程 服装设计 孙丽

score.txt存放分数信息

  学号 学科 成绩

  1001 数据结构 85
  1001 线性代数 89
  1002 数据库 100
  1002 面料工艺 30

求各学院各专业总分最高的学生。

首先分析,学号--专业是一对一的,一个学生只能属于一个专业,为简单起见, 对于并列第一名的学生,只取第一个。

Student.java

public class Student {
private int id;
private String school;
private String major;
private String name; public Student()
{ } public Student(int id,String school,String major,String name)
{
this.id = id;
this.school = school;
this.major = major;
this.name = name;
} public String toString(){
return this.id +" "+this.school +" "+ this.name+" "+this.major;
} public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }

PrintTheNo1.java

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set; public class PrintTheNo1 { public static void main(String [] args) throws IOException
{
try{
LinkedList<Student> stuList =getStuList();
HashMap<Integer,Double> map = getSumScoreOfStudent();
getTopStu(stuList,map);
}catch(IOException io)
{
System.out.println("出错了呀!"+io);
}
}
//读取info文件
public static LinkedList<Student> getStuList() throws IOException
{
String oneLine;
try{
FileReader a = new FileReader("D:\\NewPro\\Files\\info.txt");
BufferedReader br = new BufferedReader(a);
int line = 0;
LinkedList<Student> stuList = new LinkedList<Student>();
while((oneLine = br.readLine() )!= null)
{
//从文件的第二行开始读取数据
line++;
if(line == 1) continue;
else
{
String[] info = oneLine.split(" ");
Student stu = new Student(Integer.parseInt(info[0]),(String)info[1],(String)info[2],(String)info[3]);
stuList.add(stu);
}
}
for(Student s:stuList)
System.out.println(s.toString());
br.close();
return stuList;
}catch(IOException io)
{
System.out.println("error@"+io);
return null;
}
} //求各个学生的总分
public static HashMap<Integer,Double> getSumScoreOfStudent() throws IOException
{
HashMap<Integer,Double> map = new HashMap<Integer,Double>();
String oneLine;
try{ FileReader a = new FileReader("D:\\NewPro\\Files\\score.txt");
BufferedReader br = new BufferedReader(a);
int line = 0;
double sum = 0; while((oneLine = br.readLine() )!= null)
{
++line;
if(line == 1) continue;
else
{
String [] info = oneLine.split(" ");
int id = Integer.parseInt(info[0]);
if(map.containsKey(id))
{
sum = map.get(id)+Double.parseDouble(info[2]);
map.put(id,sum);
}
else
map.put(id,Double.parseDouble(info[2]));
}
} return map; }catch(IOException io)
{
System.out.println("error in score.txt"+io);
return null;
}
}
//各个专业第一名的学生
public static void getTopStu(LinkedList<Student> stuList,HashMap<Integer,Double> map)
{
Student s = new Student();
int id =0;
double score = 0;
double maxScore = 0;
String major;
//MajorAndScore保存专业--最高分
HashMap<String,Double> MajorAndScore = new HashMap<String,Double>();
//result保存 专业--学号,然后遍历result,根据result的学号值找到Student信息,保存到topStudent链表中。
HashMap<String,Integer> result = new HashMap<String,Integer>();
LinkedList<Student> topStudent = new LinkedList<Student>(); if(stuList.size() == 0 || map.size() ==0 )
return;
for(int i=0;i<stuList.size();i++)
{
s = stuList.get(i);
id =s.getId();
score = map.get(id);//拿到该学生的总分
major = s.getMajor();
if(map.containsKey(id) )
{
if(MajorAndScore.containsKey(major) && score > maxScore)
//如果已经有major.且当前学生的总分更大一些,把更大的分数加进去
{
maxScore = score;
MajorAndScore.put(major,maxScore);
result.put(major,id); } else if(!MajorAndScore.containsKey(major))
//如果不存在major key
{
MajorAndScore.put(major,score);
result.put(major,id); }
else
{
map.remove(id);
}
}
}
@SuppressWarnings("rawtypes")
Set st = result.entrySet();
Iterator it =st.iterator();
while(it.hasNext())
{ Map.Entry entry = (Map.Entry)it.next();
topStudent.add(getById(stuList,(Integer)entry.getValue()));
} System.out.println( "各专业第一名是: ");
for(int i=0;i<topStudent.size();i++)
{ System.out.println(topStudent.get(i).toString());
} } //根据id找到学生信息
private static Student getById(LinkedList<Student> stuList,int id)
{
for(int i=0;i<stuList.size();i++)
{
if(stuList.get(i).getId() == id)
{
return stuList.get(i);
}
}
return null;
} }

最新文章

  1. Fd.Service 轻量级WebApi框架
  2. arcgis_engine_develop_error_42
  3. Java EE开发平台随手记4——Mybatis扩展3
  4. js的单引号,双引号,转移符
  5. java07课堂作业
  6. [转]Java静态方法为什么不能访问非静态方法
  7. Android开源库--Universal Image Loader通用图片加载器
  8. 【最大流】【HDU3572】Task Schedule
  9. 浅谈Windows下SVN在Android Studio中的配置、基本使用及解除关联
  10. php精简完全小结(linux/laravel篇)
  11. linux 做了raid后,硬盘坏了更换问题
  12. 关于iframe的一些操作
  13. oracle密码过期问题解决
  14. POJ 3020 -Antenna Placement-二分图匹配
  15. ARM架构相关学习归纳总结
  16. rsync: read error: Connection reset by peer (104)
  17. 带状态论文粗读(三)[引用openstate的相关论文阅读]
  18. hydra 安装和使用
  19. TJU Problem 1644 Reverse Text
  20. Django(图书管理系统2)

热门文章

  1. 五分钟,让你明白MySQL是怎么选择索引《死磕MySQL系列 六》
  2. Linux部署Apollo+.Net Core简单使用
  3. .net core 和 WPF 开发升讯威在线客服系统:把 .Net Framework 打包进安装程序
  4. JMeter学习记录收藏
  5. js事件常用操作、事件流
  6. IDEA常用优化设置
  7. 在dotnet6发布之际,FastNat内网穿透,给开发人员送的硬货福利
  8. 寒武纪加速平台(MLU200系列) 摸鱼指南(二)--- 模型移植-环境搭建
  9. 网页视频不能自动播放?HTML5 video报错Uncaught (in promise) DOMException解决方法
  10. python实现图片色素的数值运算(加减乘除)和逻辑运算(与或非异或)