额,咋没了呢- package com.itheima;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.util.ArrayList;
- public class Text4 {
- /**
- * 第4题 有五个学生,每个学生有3门课(语文、数学、英语)的成绩,写一个程序接收从键盘输入学生的信息,
- * 输入格式为:name,30,30,30(姓名,三门课成绩),然后把输入的学生信息按总分从高到低的顺序写入到
- * 一个名称"stu.txt"文件中。要求:stu.txt文件的格式要比较直观,打开这个文件,就可以很清楚的看到学生的信息。
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- //创建ArrayList对象,用来存放学生对象
- ArrayList<Student> list = new ArrayList<Student>();
- list = getStudentsList();
- //创建输出缓冲区,用来将得到的学生信息写入stu.txt文件中
- BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("stu.txt")));
- for(int i = 0; i < list.size(); i ++) {
- String str = list.get(i).toString();
- bufw.write(str);
- bufw.newLine();
- bufw.flush();
- }
- bufw.close();
- }
- //定义方法,对存放学生的ArrayList按照学生总成绩排序
- public static void sort(ArrayList<Student> list) {
- Student temp = null;
- for(int i = 0; i < list.size()-1; i ++) {
- for(int j = i+1; j < list.size(); j ++) {
- if(list.get(i).getSum() < list.get(j).getSum()) {
- temp = list.get(i);
- list.set(i, list.get(j));
- list.set(j, temp);
- }
- System.out.println(""+i+j);
- }
- }
- }
- //定义方法,将键盘输入的学生对象存储到ArrayList列表中,并返回该列表
- public static ArrayList<Student> getStudentsList() throws IOException {
- System.out.println("请按照格式输入学生信息:注意学生成绩精确到小数点后一位");
- System.out.println("格式:学生名,语文成绩,数学成绩,英语成绩;其中,为英文,");
- //创建ArrayList对象,用来存放学生对象
- ArrayList<Student> list = new ArrayList<Student>();
- //创建输入缓冲区,用来就收键盘输入的学生信息
- BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
- String line = null;
- //定义正则规则,用来限制输入的学生信息的格式
- String str = "((\\w+)|([\\u4e00-\\u9fa5]+))(,((\\d{1,3})|(\\d{1,2}\\.\\d{1}))){3}";
- while((line = bufr.readLine()) != null) {
- if("over".equals(line)) {
- break;
- }
- if(!line.matches(str)) {
- System.out.println("对不起,您输入的格式不正确,请重新输入!");
- }else {
- //通过split方法,将学生的信息提取处理
- String name = line.split(",")[0];
- double chineseScore = Double.parseDouble(line.split(",")[1]);
- double mathsScore = Double.parseDouble(line.split(",")[2]);
- double englishScore = Double.parseDouble(line.split(",")[3]);
- //创建与输入信息对应的学生对象
- Student stu = new Student(name, chineseScore, mathsScore, englishScore);
- //将学生对象添加到list中
- list.add(stu);
- }
- }
- bufr.close();
- sort(list);//对list按照学生成绩进行排序
- return list;
- }
- }
- /*
- * 定义学生类
- */
- class Student {
- //定义学生类属性
- private String name;//学生姓名
- private double chineseScore;//语文成绩
- private double mathsScore;//数学成绩
- private double englishScore;//英语成绩
- //定义构造函数
- public Student(String name, double chineseScore, double mathsScore,
- double englishScore) {
- this.name = name;
- this.chineseScore = chineseScore;
- this.mathsScore = mathsScore;
- this.englishScore = englishScore;
- }
- //定义对学生类属性进行操作的方法
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public double getChineseScore() {
- return chineseScore;
- }
- public void setChineseScore(int chineseScore) {
- this.chineseScore = chineseScore;
- }
- public double getMathsScore() {
- return mathsScore;
- }
- public void setMathsScore(int mathsScore) {
- this.mathsScore = mathsScore;
- }
- public double getEnglishScore() {
- return englishScore;
- }
- public void setEnglishScore(int englishScore) {
- this.englishScore = englishScore;
- }
- //定义方法,获得该学生的总成绩。
- public double getSum() {
- return chineseScore + mathsScore + englishScore;
- }
- //用重写的方式,自定义toString方法
- @Override
- public String toString() {
- return name + ", \t" + chineseScore + ", \t" + mathsScore + ", \t" + englishScore;
- }
- }
复制代码 |