黑马程序员技术交流社区

标题: 有五个学生,每个学生有3门课(语文、数学、英语)的成... [打印本页]

作者: 橘子果酱    时间: 2015-11-20 00:06
标题: 有五个学生,每个学生有3门课(语文、数学、英语)的成...
本帖最后由 橘子果酱 于 2015-11-20 00:09 编辑

需求:
*    有五个学生,每个学生有3门课(语文、数学、英语)的成绩,写一个程序接收从键盘输入学生的信息,
*                 输入格式为:name,30,30,30(姓名,三门课成绩),
*                       然后把输入的学生信息按总分从高到低的顺序写入到一个名称"stu.txt"文件中。
*         要求:stu.txt文件的格式要比较直观,打开这个文件,就可以很清楚的看到学生的信息。
*
* 分析:
* 创建 学生类
*                 成员变量:姓名,语文成绩,数学成绩,英语成绩,总成绩
*                 成员方法:空参,有参,有参构造中传入姓名,语文成绩,数学成绩,英语成绩      
* 创建键盘录入对象
* 输入格式为:name,30,30,30
* 创建TreeSet集合,传入比较器
* 如果录入的小于5就储存在集合中
* 将录入的字符串切割成字符串数组从逗号开始切割,后面的转换为int类型
* 将转换后的结果封装成对象添加到集合中
* 遍历集合

创建 学生类
  1. package com.bean;
  2. <span style="background-color: rgb(255, 255, 255);">//创建 学生类</span>
  3. public class Student {
  4.         private String name;
  5.         private int chinese;
  6.         private int math;
  7.         private int english;
  8.         private int sum;
  9.        
  10.         public Student(){}
  11.        
  12.         public Student(String name, int chinese, int math, int english){
  13.                 super();
  14.                 this.name = name;
  15.                 this.chinese = chinese;
  16.                 this.math = math;
  17.                 this.english = english;
  18.                 this.sum = this.chinese + this.math + this.english;
  19.         }
  20.        
  21.         public int getSum(){
  22.                 return sum;
  23.         }
  24.        
  25.         public String toString(){
  26.                 return name+","+chinese+","+math+","+english+",总成绩:"+sum;
  27.                
  28.         }
  29.        
  30.        
  31. }
复制代码

  1. package com.heima;

  2. import java.io.BufferedWriter;
  3. import java.io.FileOutputStream;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.Comparator;
  7. import java.util.Scanner;
  8. import java.util.TreeSet;
  9. import com.bean.Student;

  10. /*
  11. * 创建键盘录入对象
  12. * 输入格式为:name,30,30,30
  13. * 创建TreeSet集合,传入比较器
  14. * 如果录入的小于5就储存在集合中
  15. * 将录入的字符串切割成字符串数组从逗号开始切割,后面的转换为int类型
  16. * 将转换后的结果封装成对象添加到集合中
  17. * 遍历集合
  18. */
  19. public class Demo_ReaderStudent {

  20.         public static void main(String[] args) throws IOException {
  21.                 getMassage();
  22.         }
  23.         
  24.         public static void getMassage() throws IOException{
  25.                 Scanner sc = new Scanner(System.in);
  26.                 System.out.println("请输入学生信息,输入格式为:name,30,30,30");
  27.                 TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>(){                        
  28.                         public int compare(Student s1, Student s2) {
  29.                                 int num = s1.getSum() - s2.getSum();                        
  30.                                 return num == 0? 1 :num;
  31.          }               
  32.                 });
  33.                 while(ts.size()<1){
  34.                         String line = sc.nextLine();
  35.                         String[] arr;
  36.                         int chinese;
  37.                         int math;
  38.                         int english;
  39.                         try {
  40.                                 arr = line.split("\\,");
  41.                                 chinese = Integer.parseInt(arr[1]);
  42.                                 math = Integer.parseInt(arr[2]);
  43.                                 english = Integer.parseInt(arr[3]);
  44.                                 ts.add(new Student(arr[0],chinese,math,english));
  45.                         } catch (NumberFormatException e) {
  46.                                 System.out.println("录入格式错误,请重新录入。。。。");        
  47.           }
  48.                 }
  49.                 BufferedWriter bw = new BufferedWriter(new FileWriter("stu.txt"));
  50.                 for(Student s : ts){
  51.                         bw.write(s.toString());
  52.                         bw.newLine();
  53.                 }
  54.                 bw.close();        
  55.              }
  56. }
复制代码







作者: zypt0218    时间: 2015-11-20 00:37
感觉在哪里见过这样一个题!!! 是在哪里呢? ............
作者: wx_YXxWrUwd    时间: 2015-11-20 00:48
集合还没学到啊 提前过过眼瘾 哈哈
作者: paulchoi1    时间: 2015-11-20 00:48
这个是集合题是吧!
作者: nice非飞    时间: 2015-11-20 01:01
学习Set集合的课程上有这道练习题
作者: 深寒丶    时间: 2015-11-20 01:22
Io学的不错,向你学习

作者: 传奇查    时间: 2015-11-20 02:54
算是很综合的问题了,考了正则,集合和IO~学霸~给你三十二个赞~
作者: xiao15779706    时间: 2015-11-20 08:40
还没有学到  先收藏了 总会用到
作者: ln0491    时间: 2015-11-20 09:00
学习了。。。。。。。。。。。
作者: yubail    时间: 2015-11-20 10:26
谢谢分享
作者: qcl_1989    时间: 2015-11-20 11:17
看完之后,觉得学的还是不行,还得练啊。。
作者: 蓦然回首102    时间: 2015-11-20 22:00
赞,厉害!!!
作者: songjianzaina    时间: 2015-11-21 14:48
过来学习一下~
作者: 隔壁马良    时间: 2015-11-21 15:19
我应该知道你来自哪里,不过用到集合和io了,牛
作者: kunsongjack    时间: 2015-11-21 17:50
大神  请带我飞吧
作者: 小笨笨天    时间: 2015-11-21 19:49
对于面向对象又学了一遍
作者: z13561291    时间: 2015-11-21 22:40
学习一下  思路总理不清
作者: cuiwenle    时间: 2016-3-26 23:12
用集合做的  
作者: lyoivneg    时间: 2016-4-11 14:23
zypt0218 发表于 2015-11-20 00:37
感觉在哪里见过这样一个题!!! 是在哪里呢? ............

在java基础第17天 第19小题   老师讲的 但是没用加io流
作者: lyoivneg    时间: 2016-4-11 14:25
package com.itheima.eaxm;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class Result {

        /**
         * @param args
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
                // TODO Auto-generated method stub
       Scanner sc = new Scanner(System.in);
       System.out.println("请输入 姓名,语文,数学,英语");
       TreeSet<Student> studentSet = new TreeSet<Student>(new Comparator<Student>() {

                @Override
                public int compare(Student s1, Student s2) {
                        // TODO Auto-generated method stub
                        int num = s2.getSum()-s1.getSum();
                        return num==0  ? 1 : num;
                }
              
        });
       while(studentSet.size()<5){
              
                 int china = 0;
                 int math =0;
                 int english =0 ;
             try{
                     String line = sc.nextLine();
                 String[] arr = line.split(",");
                  china = Integer.parseInt(arr[1]);
                  math = Integer.parseInt(arr[2]);
                  english = Integer.parseInt(arr[3]);
             
               int sum = china+math+english;          
               studentSet.add(new Student(arr[0],china,math,english, sum));
          }catch (Exception e) {
                        // TODO: handle exception
                  throw new RuntimeException("录入格式或字符串错误");
                }
       }  
       System.out.println();
       BufferedWriter bw = new BufferedWriter(new FileWriter("stu.txt"));
          for(Student stu : studentSet){
                  bw.write(stu.toString());
                  bw.newLine();
          }
          bw.close();
            
        }

}
作者: 地二血    时间: 2016-4-24 14:03
为什么我自己将学生类自身具备比较性,得出的结果是错误的
作者: 地二血    时间: 2016-4-24 14:11
lyoivneg 发表于 2016-4-11 14:23
在java基础第17天 第19小题   老师讲的 但是没用加io流

哥们,怎么加技术分啊




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2