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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈延真 中级黑马   /  2013-6-9 23:14  /  1655 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 陈延真 于 2013-6-11 19:04 编辑

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

public class StudentTest {
public static void main(String[] args) throws IOException {
  // 首先定义一个TreeSet,用于存储学生
  TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
   public int compare(Student s1, Student s2) {

    int num = (s2.getChinese() + s2.getMath() + s2.getEnglish()) - (s1.getChinese() + s1.getMath() + s1.getEnglish());
    int num2 = (num == 0) ? (s1.getName().compareTo(s2.getName())): num;
    return num2;
   }
  });
  // 封装键盘录入
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("请输入格式为:姓名,语文成绩,数学成绩,英语成绩:");
  for (int x = 0; x < 5; x++) {
   String line = br.readLine();
     // 创建学生对象
   Student s = new Student();
   s.setName(strArray[0]);
   s.setChinese(Integer.parseInt(strArray[1]));
   s.setMath(Integer.parseInt(strArray[2]));
   s.setEnglish(Integer.parseInt(strArray[3]));
   // 添加学生
   ts.add(s);
  }
  br.close();
  // 写入文本文件
  PrintWriter pw = new PrintWriter(new FileWriter("stu.txt"));
  pw.write("姓名\t语文成绩\t数学成绩\t英语成绩");
  // 遍历TreeSet
  for (Student s : ts) {
   pw.write(s.getName() + "\t" + s.getChinese() + "\t" + s.getMath()
     + "\t" + s.getEnglish());
     }
  pw.close();
  System.out.println("学生录入完毕");
}
}

评分

参与人数 1技术分 +1 收起 理由
黑马伍哲沂 + 1 神马都是浮云

查看全部评分

2 个回复

倒序浏览
没有学生类代码。

没有出现的具体问题描述。

也不知道strArray这个数组是从哪里来的。
回复 使用道具 举报
  1. import java.io.*;
  2. import java.util.*;
  3. class Student
  4. {
  5.         private int ma,cn,en;
  6.         private String name;
  7.         private int sum;
  8.     Student(String name,int ma,int cn,int en)
  9.         {
  10.                 this.name = name;
  11.                 this.ma = ma;
  12.                 this.cn = cn;
  13.                 this.en = en;
  14.                 sum = ma+cn+en;
  15.         }
  16.     public int compareTo(Student s)
  17.         {
  18.          int num = new Integer(this.sum).compareTo(new Integer(s.sum));
  19.                  if(num == 0)
  20.                          return this.name.compareTo(s.name);
  21.         }
  22.         public int hashCode()
  23.         {
  24.              return name.hashCode+sum*26;
  25.         }
  26.     public boolean equales(Object obj)
  27.         {
  28.          if(!(obj instanceof Student))
  29.                          throw new ClassCastException("类型不匹配");
  30.           Student s = (Student)obj;
  31.                  return this.name.equals(s.name)&&this.sum == s.sum;
  32.         }
  33.         public String toString()
  34.         {
  35.                return "student["+name+","+ma+","+cn+","+en+"]";
  36.         }

  37. }
  38. class StudentInfotool
  39. {
  40.    public static Set<Student> getStudent() throws IOException
  41.   {
  42.       BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
  43.           String line = null;
  44.           Set<Student> stus = new TreeSet<Student>();
  45.           while((lend=buff.readLine())!=null)
  46.           {
  47.              if("over".equeals(line))
  48.                          break;
  49.          String[] info = line.spilt(",");
  50.          Student stu = new Student(info[0],Integer.parseInt(info[1]),Integer.parseInt(info[2]),Integer.parseInt(info[3]));
  51.                  stus.add(stu);
  52.           }
  53.        buff.close();
  54.        return stus;
  55.    }
  56.            public static void write2File(Set<Student> stus)
  57.           {
  58.              BufferedWriter bufu = BufferedWriter(new FileWriter ("stuinfo.txt"));
  59.                  for(Student stu : stus)
  60.                  {
  61.             bufu.Write(stu.getSum()+"");
  62.                         bufu.Write(stu.toString());
  63.                         bufu.newLine();
  64.                         bufu.flush();
  65.                  }

  66.            }
  67. }


  68. class  StudentInfotest
  69. {
  70.         public static void main(String[] args) throws IOException
  71.         {
  72.                 Set<Student> stus = StudentInfotoo.getlStudent();
  73.                 StudentInfotool.write2File(stus);
  74.         }
  75. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
黑马伍哲沂 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马