qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

java io 根据TXT 在控制台上输出相关表的信息

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TestCut {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
TestCut test = new TestCut();
List<Student> list = test.readFile("D:\\Students.txt", "GBK");
test.printAver(list);
System.out.println("-------------------------------------------");
test.printExcel(list);
System.out.println("---------------------------------------------");
test.printEnglishAvg(list);
}
/**
* 按照平均分降序格式化输出
* @param list
*/
public void printAver(List<Student> list) {
if (list != null) {
String top = "序号\t学号\t平均分\t数学\t语文\t英语";
System.out.println(top);
Collections.sort(list, new Comparator<Student>() {
// 根据平均数来进行比较,降序排序
public int compare(Student arg0, Student arg1) {
if (arg1 == null)
return -1;
if (arg0.getAvg() < arg1.getAvg())
return 1;
else if (arg0.getAvg() == arg1.getAvg())
return 0;
else
return -1;
}
});
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
System.out.print((i + 1) + "\t" + student.getS_no() + "\t"
+ student.getAvg() + "\t");
System.out.print(student.getMaths() + "\t"
+ student.getChinese() + "\t");
if (student.getEnglish() != null)
System.out.println(student.getEnglish());
else {
System.out.println();
}
}
} else {
System.out.println("文件内容为空!");
}
}
/**
* 按照优秀率格式化输出
* @param list
*/
public void printExcel(List<Student> list) {
if (list != null) {
String top = "序号\t学号\t优秀率\t数学\t语文\t英语";
System.out.println(top);
Collections.sort(list, new Comparator<Student>() {
// 根据优秀率来进行比较,降序排序
public int compare(Student arg0, Student arg1) {
if (arg1 == null)
return -1;
if (arg0.getExcel() < arg1.getExcel())
return 1;
else if (arg0.getExcel() == arg1.getExcel())
return 0;
else
return -1;
}
});
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
DecimalFormat df = new DecimalFormat("#%");
System.out.print((i + 1) + "\t" + student.getS_no() + "\t"
+ df.format(student.getExcel()) + "\t");
System.out.print(student.getMaths() + "\t"
+ student.getChinese() + "\t");
if (student.getEnglish() != null)
System.out.println(student.getEnglish());
else {
System.out.println();
}
}
} else {
System.out.println("文件内容为空!");
}
}
/**
* 求英语平均成绩
* @param list
*/
public void printEnglishAvg(List<Student> list) {
printAvgByCourse(list, 3);
}
/**
* 求课程平均成绩,并输出
* @param list
* @param course
*        课程(1:数学,2:语文,3:英语)
*/
private void printAvgByCourse(List<Student> list, int course) {
Integer avg = 0;
switch (course) {
case 1: {
Integer maths = 0;
for (Student student : list) {
maths += student.getMaths();
}
avg = maths / list.size();
System.out.println("数学平均成绩:\t" + avg);
break;
}
case 2: {
Integer chinese = 0;
for (Student student : list) {
chinese += student.getChinese();
}
avg = chinese / list.size();
System.out.println("语文平均成绩:\t" + avg);
break;
}
case 3: {
Integer english = 0;
Integer size = 0;
for (Student student : list) {
if (student.getEnglish() != null) {
english += student.getEnglish();
size++;
}
}
if (size != 0)
avg = english / size;
System.out.println("英语平均成绩:\t" + avg);
break;
}
default: {
System.out.println("不存在此课程");
break;
}
}
}
/**
* 读取文件信息
* @param fileName
*            文件路径
* @param charset
*            编码
* @return
* @throws IOException
*/
public List<Student> readFile(String fileName, String charset) {
List<Student> list = new ArrayList<Student>();
FileInputStream fi = null;
BufferedReader in = null;
try {
fi = new FileInputStream(new File(fileName));
in = new BufferedReader(new InputStreamReader(fi, charset));
String result = in.readLine();
while ((result = in.readLine()) != null) {
String[] str = result.split("\t");
Student student = new Student();
for (int i = 0; i < str.length; i++) {
student.setS_no(str[0]);
student.setMaths(Integer.parseInt(str[1]));
student.setChinese(Integer.parseInt(str[2]));
if (str.length > 3) {
student.setEnglish(Integer.parseInt(str[3]));
}
student.setAvg(student.culAvg());
student.setExcel(student.culExcel());
}
list.add(student);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
}
class Student implements Serializable {
private static final long serialVersionUID = -6517638655032546653L;
/**
* 学号
*/
private String s_no;
/**
* 数学
*/
private Integer maths;
/**
* 语文
*/
private Integer chinese;
/**
* 英语
*/
private Integer english;
/**
* 平均分
*/
private Integer avg;
/**
* 优秀率
*/
private float excel;
public Student() {
}
public Student(Integer maths, Integer chinese, Integer english) {
this.maths = maths;
this.chinese = chinese;
this.english = english;
this.avg = culAvg(maths, chinese, english);
this.excel = culExcel(maths, chinese, english);
}
public String getS_no() {
return s_no;
}
public void setS_no(String sNo) {
s_no = sNo;
}
public Integer getMaths() {
return maths;
}
public void setMaths(Integer maths) {
this.maths = maths;
}
public Integer getChinese() {
return chinese;
}
public void setChinese(Integer chinese) {
this.chinese = chinese;
}
public Integer getEnglish() {
return english;
}
public void setEnglish(Integer english) {
this.english = english;
}
public Integer getAvg() {
return avg;
}
public void setAvg(Integer avg) {
this.avg = avg;
}
public float getExcel() {
return excel;
}
public void setExcel(float excel) {
this.excel = excel;
}
public String toString() {
StringBuffer sb = new StringBuffer("[");
sb.append("s_no=" + s_no).append(",maths=").append(maths).append(
",chinese=").append(chinese).append(",english=")
.append(english).append(",avg=").append(avg).append(",excel=")
.append(excel).append("]");
return sb.toString();
}
/**
* 计算平均数
* @param maths
*            数学
* @param chinese
*            语文
* @param english
*            英语
* @return
*/
private Integer culAvg(Integer maths, Integer chinese, Integer english) {
// 计算平均分
Integer sum = chinese + maths;
float aver = english == null ? (float) sum / 2
: (float) (sum + english) / 3;
return Math.round(aver);
}
/**
* 计算优秀率
* @param maths
*            数学
* @param chinese
*            语文
* @param english
*            英语
* @return
*/
private float culExcel(Integer maths, Integer chinese, Integer english) {
final Integer EXCEL_NUMBER = 85;
Integer total_number = english == null ? 2 : 3;
Integer ex = 0;
if (maths >= EXCEL_NUMBER)
ex++;
if (chinese >= EXCEL_NUMBER)
ex++;
if (english != null && english >= EXCEL_NUMBER)
ex++;
return (float) ex / total_number;
}
/**
* 计算平均数
* @return
*/
public Integer culAvg() {
return culAvg(this.maths, this.chinese, this.english);
}
/**
* 计算优秀率
* @return
*/
public float culExcel() {
return culExcel(this.maths, this.chinese, this.english);
}
}

posted on 2011-09-21 22:13 顺其自然EVO 阅读(485) 评论(0)  编辑  收藏

<2011年9月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜