昨天去一公司应聘,笔试遇到一道题目3、有一个文本文件,以以下格式纪录了每个学生的考试成绩,
Jam 语文 90 数学 95 英语99 计算机 80
Steven 语文 100 数学90 英语90 计算机90
Roger 语文80 数学100 英语90 计算机100
……
请写一段程序,从文本中读取出每个学生的成绩,并
a) 计算每个学生的总分和平均分,并按照总分由高到低进行排序,将排序好的数据输出到student_score.txt中,每一行一条纪录;(6分)
b) 对每个科目按照分数由高到低进行排序,并将排序号的数据输出到相应的文本文件中,每一行一条纪录,在最后一行给出平均分。(7分)我立马想到用BufferedReader每读一行以空格截串,结果做到后来很麻烦,最后也没有时间了。谁有没有好的方法解决~~
Jam 语文 90 数学 95 英语99 计算机 80
Steven 语文 100 数学90 英语90 计算机90
Roger 语文80 数学100 英语90 计算机100
……
请写一段程序,从文本中读取出每个学生的成绩,并
a) 计算每个学生的总分和平均分,并按照总分由高到低进行排序,将排序好的数据输出到student_score.txt中,每一行一条纪录;(6分)
b) 对每个科目按照分数由高到低进行排序,并将排序号的数据输出到相应的文本文件中,每一行一条纪录,在最后一行给出平均分。(7分)我立马想到用BufferedReader每读一行以空格截串,结果做到后来很麻烦,最后也没有时间了。谁有没有好的方法解决~~
String str;
// 读取每一行
str = br.readLine();
if (str == null) {
break;
}
// 获取成绩
String grade[] = str.split("\\D+");
// 总分
Double sum = 0.0;
// 平均分
Double avg = 0.0;
for (int i = 1; i < grade.length; i++) {
sum += Double.parseDouble(grade[i]);
}
avg = sum / grade.length;
map.put(str, sum);
String gradeStr = str + ",总分" + sum + ",平均分" + avg;
package org.lkm.test;import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;public class readText { /**
* @author longkm
* @param args
* @create_date 2011-5-20
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
File file = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
file = new File("d:\\Students.txt");
br = new BufferedReader(new FileReader(file));
bw = new BufferedWriter(new FileWriter(new File(
"d:\\student_score.txt")));
Map<String, Double> map = new TreeMap<String, Double>();
while (true) {
String str;
// 读取每一行
str = br.readLine();
if (str == null) {
break;
}
// 获取成绩
String grade[] = str.split("\\D+");
// 总分
Double sum = 0.0;
// 平均分
Double avg = 0.0;
for (int i = 1; i < grade.length; i++) {
sum += Double.parseDouble(grade[i]);
}
avg = sum / grade.length;
map.put(str + " 平均分" + avg, sum);
}
//按value排序
List arrayList = new ArrayList(map.entrySet());
Collections.sort(arrayList, new Comparator() {
public int compare(Object o1, Object o2) {
Map.Entry obj1 = (Map.Entry) o1;
Map.Entry obj2 = (Map.Entry) o2;
return ((Double) obj1.getValue()).compareTo((Double) obj2
.getValue());
}
}); //按总分由高到低写入文件
for (int i = arrayList.size()-1; i >=0 ; i--) {
String str = ((Map.Entry) arrayList.get(i)).getKey()+ " 总分" + ((Map.Entry) arrayList.get(i)).getValue();
bw.write(str);
bw.newLine();
} } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
br.close();
bw.flush();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}}这是第一题,测试没已通过
package com.liqi.test;import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;/**
* 3、有一个文本文件,以以下格式纪录了每个学生的考试成绩,
Jam 语文 90 数学 95 英语99 计算机 80
Steven 语文 100 数学90 英语90 计算机90
Roger 语文80 数学100 英语90 计算机100
……
请写一段程序,从文本中读取出每个学生的成绩,并
a) 计算每个学生的总分和平均分,并按照总分由高到低进行排序,将排序好的数据输出到student_score.txt中,每一行一条纪录;(6分)
b) 对每个科目按照分数由高到低进行排序,并将排序号的数据输出到相应的文本文件中,每一行一条纪录,在最后一行给出平均分。(7分)
*/
public class TestB {
public static void readFile(){
List students = new ArrayList();
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(new File("./com/liqi/test/student.txt")));
String temp = "";
while((temp = in.readLine()) != null){
//以字符分割
String[] tempArrayAll = temp.split("\\W");
String[] tempArray = new String[5];
int count = 0;
for(int i = 0 ; i < tempArrayAll.length; i ++){
if(tempArrayAll[i] != null && !tempArrayAll[i].trim().equals("")){
tempArray[count] = tempArrayAll[i];
count ++;
}
}
Student s = new Student();
s.setName(tempArray[0]);
s.setChineseScore(tempArray[1]);
s.setMathScore(tempArray[2]);
s.setEnglishScore(tempArray[3]);
s.setComputerScore(tempArray[4]);
s.setTotalScore(getTotalScore(s));
s.setPScore(getPScore(s));
students.add(s);
}
} catch (Exception e) {
e.printStackTrace();
}
Collections.sort(students);
Collections.reverse(students);
//写入文件STUDENTB.TXT
for(int i = 0; i < students.size(); i ++){
wirteToFile(students, "student_score.txt");
}
//按照分科排序,并写入不同的文件
try {
writeToBranchFile(students, "ChineseScore", "语文.txt");
writeToBranchFile(students, "EnglishScore", "英语.txt");
writeToBranchFile(students, "MathScore", "数学.txt");
writeToBranchFile(students, "ComputerScore", "电脑.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
private static void writeToBranchFile(List students, String string, String fileName) throws Exception {
//通过反射获得get方法
Method t = Student.class.getMethod("get" + string, null);
Method t2 = Student.class.getMethod("getName", null);
List simpleStudentsList = new ArrayList();
for(int i = 0; i < students.size(); i ++){
Student s = (Student)students.get(i);
String score = (String)t.invoke(s, null);
String name = (String)t2.invoke(s, null);
SimpleStudent ss = new SimpleStudent();
ss.setName(name);
ss.setScore(new BigDecimal(score));
simpleStudentsList.add(ss);
}
Collections.sort(simpleStudentsList);
Collections.reverse(simpleStudentsList);
wirteToFile(simpleStudentsList, fileName);
} private static void wirteToFile(List students, String string) {
BufferedWriter out = null;
try{
out = new BufferedWriter(new FileWriter(new File(string)));
for(int i =0 ; i < students.size(); i ++){
out.write(students.get(i).toString());
out.write('\n');
}
out.flush();
out.close();
}catch(Exception ex){
ex.printStackTrace();
}
} /**
* 获得平均分
*/
private static BigDecimal getPScore(Student s) {
BigDecimal totalScore = s.getTotalScore();
BigDecimal pScore = totalScore.divide(new BigDecimal(4));
return pScore;
}
/**
* 获得总分
*/
private static BigDecimal getTotalScore(Student s) {
int totalScore = Integer.parseInt(s.getChineseScore()) + Integer.parseInt(s.getComputerScore())
+ Integer.parseInt(s.getEnglishScore()) + Integer.parseInt(s.getMathScore());
return new BigDecimal(totalScore);
} public static void main(String args[]){
readFile();
}
}
package com.liqi.test;import java.math.BigDecimal;public class SimpleStudent implements Comparable{
private String name;
private BigDecimal score; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public BigDecimal getScore() {
return score;
} public void setScore(BigDecimal score) {
this.score = score;
}
public int compareTo(Object arg0) {
SimpleStudent s = (SimpleStudent)arg0;
if(this.getScore().compareTo(s.getScore()) > 0){
return 1;
}
return 0;
}
public String toString(){
return this.getName() + " : " + this.getScore();
}
}
package com.liqi.test;import java.math.BigDecimal;public class Student implements Comparable{
private String name;
private String chineseScore;
private String mathScore;
private String englishScore;
private String computerScore;
private BigDecimal totalScore;
private BigDecimal pScore;
public String getChineseScore() {
return chineseScore;
} public void setChineseScore(String chineseScore) {
this.chineseScore = chineseScore;
} public String getComputerScore() {
return computerScore;
} public void setComputerScore(String computerScore) {
this.computerScore = computerScore;
} public String getEnglishScore() {
return englishScore;
} public void setEnglishScore(String englishScore) {
this.englishScore = englishScore;
} public String getMathScore() {
return mathScore;
} public void setMathScore(String mathScore) {
this.mathScore = mathScore;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public BigDecimal getPScore() {
return pScore;
} public void setPScore(BigDecimal score) {
pScore = score;
} public BigDecimal getTotalScore() {
return totalScore;
} public void setTotalScore(BigDecimal totalScore) {
this.totalScore = totalScore;
} public int compareTo(Object arg0) {
Student s = (Student) arg0;
if(this.totalScore.compareTo(s.getTotalScore()) > 0){
return 1;
}else if(this.pScore.compareTo(s.getPScore()) > 0){
return 1;
}
return 0;
} public String toString(){
return this.name + " 语文 " + this.getChineseScore() + " 数学 " + this.getMathScore() + " 英语 "
+ this.getEnglishScore() + " 计算机 " + this.getComputerScore() + " 总分:" + this.getTotalScore() + " 平均分:" + this.getPScore();
}
}
//用这结构好一点,代码有空补上
Student{
String 名字;
Map<String 科目,Double 成绩> 成绩;
}
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class testPublicString{
private static final char Space = ' ';
Set<String> commonChildStrSet; public testPublicString(String str1,String str2){
str1=getPrefix(str2.length())+str1; commonChildStrSet=new HashSet<String>();
for(int i=0;i<str1.length();i++){
String childStr=getSubString(str1,i, str2.length());
String commonStr=getCommonString(childStr,str2);
commonChildStrSet.addAll(getSplitResult(commonStr));
}
}
public List<String> getSplitResult(String str){
List<String> ls=new ArrayList<String>();
str=str.trim();
String[] arr=str.split("\\s+");
for(String tmp:arr){
if(tmp.length()>0){
ls.add(tmp);
}
}
return ls;
}
private String getPrefix(int n){
StringBuffer sb=new StringBuffer();
for(int i=0;i<n;i++){
sb.append(Space);
}
return sb.toString();
}
public String getCommonString(String op1,String op2){
StringBuffer sb=new StringBuffer();
for(int i=0;i<op1.length();i++){
char c1=op1.charAt(i);
char c2=op2.charAt(i);
sb.append(c1==c2?c1:Space);
}
return sb.toString();
}
private String getSubString(String str,int startIndex,int length){
String strTmp=str.substring(startIndex);
int n=strTmp.length();
return strTmp.substring(0, length>n?n:length);
}
public Set<String> getCommonChildStrSet() {
return commonChildStrSet;
}
public static void main(String[] args){
System.out.println("run!Please input two Strings!");
Scanner test001 = new Scanner(System.in);
Scanner test002 = new Scanner(System.in);
String test1=test001.nextLine();
String test2=test002.nextLine();
testPublicString commonChildString=new testPublicString(test1,test2);
// run result:
System.out.println("run result:");
System.out.print(test1+"和"+test2+"的匹配字串有:");
for(String tmp:commonChildString.getCommonChildStrSet()){
System.out.print(tmp+",");
}
System.out.print("\n");
}
}