A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© chenxin2015 中级黑马   /  2015-4-25 22:02  /  518 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

有五个学生,每个学生有3门课(语文、数学、英语)的成绩,写一个程序接收从键盘输入学生的信息,
* 输入格式为:name,30,30,30(姓名,三门课成绩),然后把输入的学生信息按总分从高到低的顺序写入到一个名称"stu.txt"文件中。
要求:stu.txt文件的格式要比较直观,打开这个文件,就可以很清楚的看到学生的信息

2 个回复

倒序浏览
王烽棋 来自手机 中级黑马 2015-4-25 23:12:45
沙发
chenxin2015 发表于 半小时前
有五个学生,每个学生有3门课(语文、数学、英语)的成绩,写一个程序接收从键盘输入学生的信息,
* 输入格式为:name,30,30,30(姓名,三门课成绩),然后把输入的学生信息按总分从高到低的...

分享下思路:数据均与学生有关,故按照面向对象的选择创建学生类存储基本数据。学生对象也是数据,创建集合存储学生对象,运用集合工具类的排序方法,或者使用set集合使对象本身具备比较性。来自: iPhone客户端
回复 使用道具 举报
王烽棋 来自手机 中级黑马 2015-4-25 23:34:06
藤椅
package p3;  import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator;  public class Test_IOTest {          public static void main(String[] args) throws IOException {                 /*                  * 有五个学生,每个学生有三门课的成绩,定义一种比较直观的文件格式,                  * 输入学生的姓名和成绩,输入格式:name,30,30,30从键盘输入以上数                  * 据(包括姓名,三门课成绩),按总分从高到低的顺序将学生的信息存放                  * 在硬盘文件“stu.txt”中。                  */                                  inputGread();          }         /*          * 步骤          * 1,键盘录入数据。          * 2,根据录入数据创建学生对象将数据存储。          * 3,将学生对象存入TreeSet集合。          * 4,通过流将数据存储在"stu.txt"文件中。          */          public static void inputGread() throws IOException{                 File file = new File("tempfile\\stu.txt");                 if(!file.exists())                          file.createNewFile();                                   ArrayList<Student> list = new ArrayList<Student>();                  BufferedReader        bfr = null;                  BufferedWriter bfw = null;                  try {                           bfr = new BufferedReader(new InputStreamReader(System.in));                           String line = null;                           while ((line=bfr.readLine())!=null) {                                   if ("over".equals(line))                                          break;                                   String[] data = line.split(",");                                   String name = data[0];                                   int math = Integer.parseInt(data[1]);                                   int english = Integer.parseInt(data[2]);                                   int chinses = Integer.parseInt(data[3]);                                   Student student = new Student(name,math,english,chinses);                                   list.add(student);                             }                           Collections.sort(list, new Comparator<Student>() {                                 @Override                                 public int compare(Student o1, Student o2) {                                         int temp = o2.getSumGrade()-o1.getSumGrade();                                         return temp==0?o1.getName().compareTo(o2.getName()):temp;                                 }                         });                           bfw = new BufferedWriter(new FileWriter(file));                           for (Student student : list) {                                   String data = student.toString();                                         bfw.write(data);                                         bfw.newLine();                                         bfw.flush();                         }                         } catch (IOException e) {                         }finally{                                 bfr.close();                                 if(bfw!=null)                                  bfw.close();                         }                                                    }          } class Student implements Comparable<Student>{         private String name;         private int math;         private int english;         private int chinses;         /**          * @param name          * @param math          * @param english          * @param chinses          */         public Student(String name, int math, int english, int chinses) {                 super();                 this.name = name;                 this.math = math;                 this.english = english;                 this.chinses = chinses;         }         /**          * @return the name          */         public String getName() {                 return name;         }         /**          * @param name the name to set          */         public void setName(String name) {                 this.name = name;         }         /**          * @return the math          */         public int getMath() {                 return math;         }         /**          * @param math the math to set          */         public void setMath(int math) {                 this.math = math;         }         /**          * @return the english          */         public int getEnglish() {                 return english;         }         /**          * @param english the english to set          */         public void setEnglish(int english) {                 this.english = english;         }         /**          * @return the chinses          */         public int getChinses() {                 return chinses;         }         /**          * @param chinses the chinses to set          */         public void setChinses(int chinses) {                 this.chinses = chinses;         }         /**          * @return the sumGrade          */         public int getSumGrade() {                 return english+math+chinses;         }         /* (non-Javadoc)          * @see java.lang.Object#toString()          */         @Override         public String toString() {                 return "Student" + name + ",math=" + math + ",english="                                 + english + ",chinses=" + chinses +",SumGrade"+getSumGrade()+ "]";         }         @Override         public int compareTo(Student o) {                 int temp = getSumGrade()-o.getSumGrade();                 return temp==0?name.compareTo(o.name):temp;         }          }来自: iPhone客户端
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马