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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Hiviiup 中级黑马   /  2014-8-20 22:32  /  4347 人查看  /  20 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

我感觉好像写跑题了,谁能告诉我走到这一步还能根据总分来排序么,怎么排序啊,没思路。


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


  8. public class test03 {
  9.         public static void main(String[] args) {
  10.                 BufferedWriter bw=null;
  11.                 BufferedReader br=null;
  12.                 try
  13.                 {
  14.                         br=new BufferedReader(new InputStreamReader(System.in));
  15.                         bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("stu.txt")));
  16.                         int count=1;
  17.                         System.out.println("请输入第"+count+"位学生的成绩,格式为:姓名,语文成绩,数学成绩,英语成绩");
  18.                         bw.write("姓名        "+"语文        "+"数学        "+"英语        "+"总分");
  19.                         bw.newLine();
  20.                         String s=null;
  21.                         while((s=br.readLine())!=null)
  22.                         {
  23.                                 count++;
  24.                                 if("over".equals(s))
  25.                                         break;
  26.                                 System.out.println("请输入第"+count+"位学生的成绩,格式为:姓名,语文成绩,数学成绩,英语成绩");
  27.                                 String arr[]=s.split(",");
  28.                                 int sum=0;
  29.                                 for(int x=0;x<arr.length;x++)
  30.                                 {
  31.                                         bw.write(arr[x]+"        ");
  32.                                         bw.flush();
  33.                                         if(x+1>=arr.length)
  34.                                         {
  35.                                                 bw.write(sum+"");
  36.                                                 break;
  37.                                         }
  38.                                         sum=Integer.valueOf(arr[x+1])+sum;
  39.                                        
  40.                                 }
  41.                                
  42.                                 bw.newLine();
  43.                         }
  44.                        
  45.                 }
  46.                 catch (Exception e)
  47.                 {
  48.                         throw new RuntimeException();
  49.                 }
  50.                 finally
  51.                 {
  52.                         try
  53.                         {
  54.                                 if(bw!=null)
  55.                                         bw.close();
  56.                         }
  57.                         catch(IOException e2)
  58.                         {
  59.                                 throw new RuntimeException("写入资源失败");
  60.                         }
  61.                         try
  62.                         {
  63.                                 if(br!=null)
  64.                                         br.close();
  65.                         }
  66.                         catch(IOException e2)
  67.                         {
  68.                                 throw new RuntimeException("读取资源失败");
  69.                         }
  70.                 }
  71.         }
  72.        
  73. }
复制代码

20 个回复

倒序浏览
看看  学习下
回复 使用道具 举报
学生类中还可以在设计三门成绩的总分。
回复 使用道具 举报
这题我做过。我不能给你源码。我给你一下我的思路吧。
思路:
        ① 先描述一个学生对象。让它实现Comparable接口,利用compareTo()方法来比较总分。
        ② 用TreeSet来装学生对象并用比较器排序。
        ③ 通过读取键盘输入5行数据,将输入的数据存到TreeSet集合中;
        ④ 从TreeSet中遍历每条记录,然后通过写入流输出成“stu.txt”文件。
回复 使用道具 举报
一看到“按顺序”你就该想到使用集合中的某位仁兄了
回复 使用道具 举报
额,,好像不是很好,。
回复 使用道具 举报
先写思路诶。。。我觉得这比较好。
回复 使用道具 举报
  1. /*
  2. *3. 有五个学生,每个学生有3门课(语文、数学、英语)的成绩,写一个程序接收从键盘输入学生的信息,
  3. *        输入格式为:name,30,30,30(姓名,三门课成绩),然后把输入的学生信息按总分从高到低的顺序
  4. *        写入到一个名称"stu.txt"文件中。要求:stu.txt文件的格式要比较直观,打开这个文件,就可
  5. *        以很清楚的看到学生的信息。
  6. *        
  7. *        
  8. * 思路:
  9. * 1.从键盘读取数据,将读到的每行数据写入数组
  10. * 2.将数组内容作为构造函数参数传入,创建Student对象
  11. * 3.将创建的Student对象传入TreeSet集合进行排序
  12. * 4.遍历TreeSet集合将内容取出存入stu.txt文件中
  13. */

  14. import java.io.*;
  15. import java.util.*;

  16. import static java.lang.Integer.parseInt;

  17. class Student implements Comparable<Student>
  18. {
  19.         private String name;
  20.         private int chinese;
  21.         private int math;
  22.         private int english;

  23.         public String getName()
  24.         {
  25.                 return name;
  26.         }

  27.         public int getChinese()
  28.         {
  29.                 return chinese;
  30.         }

  31.         public int getMath()
  32.         {
  33.                 return math;
  34.         }

  35.         public int getEnglish()
  36.         {
  37.                 return english;
  38.         }

  39.         public Student(String name, int chinese, int math, int english)
  40.         {
  41.                 this.math = math;
  42.                 this.chinese = chinese;
  43.                 this.english = english;
  44.                 this.name = name;
  45.         }

  46.         @Override
  47.         public int compareTo(Student o)
  48.         {
  49.                 int th = this.chinese + this.english + this.math;
  50.                 int oo = o.chinese + o.english + o.math;
  51.                 if (th < oo)
  52.                 {
  53.                         return 1;
  54.                 }
  55.                 else if(th>oo)
  56.                 {
  57.                         return -1;
  58.                 }
  59.                 else
  60.                 {
  61.                         return this.name.compareTo(o.name);
  62.                 }
  63.         }
  64. }

  65. public class StudentTest
  66. {
  67.         public static void main(String[] args)
  68.         {
  69.                 // 从键盘读取数据
  70.                 BufferedReader bd = new BufferedReader(new InputStreamReader(System.in));
  71.                 BufferedWriter bw = null;
  72.                 try
  73.                 {
  74.                         bw = new BufferedWriter(new FileWriter("d:\\stu.txt"));
  75.                         bw.write("姓名" + "\t" + "语文" + "\t" + "数学" + "\t" + "英语" + "\t"
  76.                                         + "总分");
  77.                         bw.newLine();
  78.                 } catch (IOException e1)
  79.                 {
  80.                         e1.printStackTrace();
  81.                 }
  82.                 String line = null;
  83.                 String[] str = null;
  84.                 TreeSet<Student> ts = new TreeSet<Student>();
  85.                 System.out.println("请按照\"姓名,语文,数学,英语\"的顺序输入成绩:");
  86.                 try
  87.                 {
  88.                         while ((line = bd.readLine()) != null)
  89.                         {
  90.                                 if ("over".equals(line))
  91.                                 {
  92.                                         break;
  93.                                 }
  94.                                 System.out.println("请按照\"姓名,语文,数学,英语\"的顺序输入成绩:");
  95.                                 str = line.split(",");
  96.                                 Student stu = new Student(str[0], parseInt(str[1]),
  97.                                                 parseInt(str[1]), parseInt(str[1]));
  98.                                 ts.add(stu);
  99.                         }
  100.                 } catch (IOException e)
  101.                 {
  102.                         e.printStackTrace();
  103.                 }
  104.                 Iterator<Student> it = ts.iterator();
  105.                 while (it.hasNext())
  106.                 {
  107.                         Student sd = it.next();
  108.                         int zf = sd.getChinese() + sd.getEnglish() + sd.getMath();
  109. //                        System.out.println(sd.getName() + "\t" + sd.getChinese() + "\t"+ sd.getMath() + "\t" + sd.getEnglish() + "\t" + zf);
  110.                         try
  111.                         {
  112.                                 bw.write(sd.getName() + "\t" + sd.getChinese() + "\t"+ sd.getMath() + "\t" + sd.getEnglish() + "\t" + zf);
  113.                                 bw.newLine();
  114.                                 bw.flush();
  115.                         } catch (IOException e)
  116.                         {
  117.                                 e.printStackTrace();
  118.                         }

  119.                 }
  120.                 try
  121.                 {
  122.                         bd.close();
  123.                         bw.close();
  124.                 } catch (IOException e)
  125.                 {
  126.                         // FLY 自动生成的 catch 块
  127.                         e.printStackTrace();
  128.                 }

  129.         }
  130. }
复制代码



共同学习,共同进步
回复 使用道具 举报
a6511631 发表于 2014-8-21 10:24
这题我做过。我不能给你源码。我给你一下我的思路吧。
思路:
        ① 先描述一个学生对象。让它实现Comparable ...

恩  已经弄出来了
回复 使用道具 举报
入学测试题怎么难呀  我都看蒙了啊  
回复 使用道具 举报
有点看不懂〒_〒
回复 使用道具 举报
表示默默的路过~~~~
回复 使用道具 举报
看懵了            
回复 使用道具 举报
a6511631 发表于 2014-8-21 10:24
这题我做过。我不能给你源码。我给你一下我的思路吧。
思路:
        ① 先描述一个学生对象。让它实现Comparable ...

你这里不用比较器都可以把!题目的重点在总分排名。复写compareTo方法就可以拉
回复 使用道具 举报
Hiviiup 发表于 2014-8-23 07:30
恩  已经弄出来了

你好  我按照你的思路 写到 怎么样学生类对象接收键盘输入的学生
这里我不会写了   

  1. public static void main(String[] args)
  2.         {
  3.                 Scanner in = new Scanner(System.in);
  4.                
  5.                 TreeSet<Person> tree = new TreeSet<Person>();
  6.                 while(in.hasNextLine())
  7.                 {
  8.                         Person student = in.next();
  9.                         tree.add(student);
  10.                 }
  11.                
  12.                 for(int i=0;i<tree.size();i++)
  13.                 {
  14.                         tree.add();
  15.                 }               
  16.         }
复制代码
回复 使用道具 举报
谢谢分享
回复 使用道具 举报
本帖最后由 少年闰土 于 2015-6-18 14:31 编辑
happymouse 发表于 2014-8-23 00:02
共同学习,共同进步

假如有两个学生的总分不一样姓名一样,你这个加入TreeSet的时候,直接根据总分不一样就判断元素不一样了,那会出现两个姓名相同,但总分不一样的情况
回复 使用道具 举报
本帖最后由 少年闰土 于 2015-6-18 14:31 编辑
a6511631 发表于 2014-8-21 10:24
这题我做过。我不能给你源码。我给你一下我的思路吧。
思路:
        ① 先描述一个学生对象。让它实现Comparable ...

你的比较是怎么处理的,对于一下情况能处理吗?
同一个人输了两次,但是总分不一样,能提示错误吗
回复 使用道具 举报
学习了!!!!!
回复 使用道具 举报
我只是来刷分的
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马