java计算期中考试取得最高成绩的学生姓名和所属专业,急~
发布网友
发布时间:2024-10-23 21:49
我来回答
共3个回答
热心网友
时间:2024-10-26 07:47
package export;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class TxtTest {
static List<Map<String, String[]>> allStuInfos = new ArrayList<Map<String, String[]>>();
static List<Map<String, String>> allMajorfos = new ArrayList<Map<String, String>>();
public static void main(String[] args) {
TxtTest test=new TxtTest();
test.getStudentInfo();
test.getMajorInfo();
int maxGrade=0;//最高成绩
String name=null;//最好成绩者姓名
String major=null;//最高成绩的专业
for (Map<String, String[]> studentInfo: allStuInfos) {
Set<String> sets=studentInfo.keySet();
for (String string : sets) {
String[] temstr=studentInfo.get(string);
if(Integer.parseInt(temstr[3])>maxGrade){
maxGrade=Integer.parseInt(temstr[3]);
name=temstr[1];
major=temstr[2];
}
}
}
for(Map<String, String> majorfo:allMajorfos){
if(majorfo.get(major)!=null){
major=majorfo.get(major);
}
}
System.out.println("Highest score student is "+name+" belongs to "+major);
}
public void getStudentInfo() {
FileReader student = null;
BufferedReader reader = null;
try {
student = new FileReader("D://student.txt");
reader = new BufferedReader(student);
String oneStudent = null;
while ((oneStudent=reader.readLine()) != null) {
String[] stuInfos = oneStudent.split(";");
Map<String, String[]> map = new HashMap<String, String[]>();
map.put(stuInfos[0], stuInfos);
allStuInfos.add(map);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
student.close();
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void getMajorInfo() {
FileReader major = null;
BufferedReader reader = null;
try {
major = new FileReader("D://department.txt");
reader = new BufferedReader(major);
String tempString = null;
while ((tempString=reader.readLine()) != null) {
String[] stuInfos = tempString.split(";");
Map<String, String> map = new HashMap<String, String>();
map.put(stuInfos[0], stuInfos[1]);
allMajorfos.add(map);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
major.close();
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
热心网友
时间:2024-10-26 07:48
啊啊啊啊啊啊啊阿事实上是事实上事实上是事实上
热心网友
时间:2024-10-26 07:46