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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 15836999770 中级黑马   /  2014-12-11 21:42  /  849 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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

2 个回复

倒序浏览
创建student类,然后把student传输TreeSet。student类实现comparebor接口。重新compareto方法
回复 使用道具 举报
package com.itheima;
import java.io.*;
import java.util.*;
/**
* 7,键盘录入学生的语文,数学,英语成绩。按照总分排序从高到下写入文本中
*  xxx 80 80 80
*/
public class Test7 {


        public static void main(String[] args) throws Exception{
                // TODO Auto-generated method stub
                BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
                BufferedWriter bufw = new BufferedWriter(new FileWriter(new File("C:\\Users\\16F4\\Desktop\\Test\\1.txt")));
                String len = null;
                TreeSet<students> ts = new TreeSet<>();
                while((len = bufr.readLine())!= null){
                        if("end".equals(len)){
                                break;
                        }else {
                                String[] str = len.split(" +");
                                String name = str[0];
                                int Chinese = 0;
                                int Math = 0 ;
                                int EngLish = 0;
                                Chinese = toInt(str[1]);
                                Math = toInt(str[2]);
                                EngLish = toInt(str[3]);
                                ts.add(new students(name, Chinese, Math, EngLish));
                        }
                }
                Iterator<students> it = ts.iterator();
                while(it.hasNext()){
                        students s = it.next();
                        bufw.write(s.name + " " +s.Chinese+ " " +s.Math+ " " +s.EngLish);
                        bufw.newLine();
                }
                bufr.close();
                bufw.close();
        }

        public static int toInt(String string) {
                // TODO Auto-generated method stub
                int x = 0;
                char[] c = string.toCharArray();
                //System.out.println(c.length);
                for (int i = 0; i < c.length; i++) {
                        int y = c[i] - '0';
                        for (int j = 0; j < c.length - i - 1; j++) {
                                y *= 10;
                        }
                        x += y;
                }
                //System.out.println(x);
                return x;
        }
               
}
class students implements Comparable<Object>{
        String name;
        int Chinese;
        int Math;
        int EngLish;
        public students(String name,int Chinese,int Math,int EngLish) {
                // TODO Auto-generated constructor stub
                this.name = name;
                this.Chinese = Chinese;
                this.Math = Math;
                this.EngLish = EngLish;
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public int getChinese() {
                return Chinese;
        }
        public void setChinese(int chinese) {
                Chinese = chinese;
        }
        public int getMath() {
                return Math;
        }
        public void setMath(int math) {
                Math = math;
        }
        public int getEngLish() {
                return EngLish;
        }
        public void setEngLish(int engLish) {
                EngLish = engLish;
        }
        @Override
        public int compareTo(Object obj) {
                // TODO Auto-generated method stub
                if(!(obj instanceof students))
                        throw new RuntimeException();
                       
                students s = (students)obj;
                if((this.Math+this.Chinese+this.EngLish)>(s.Chinese+ s.EngLish + s.Math))
                        return 1;
                return -1;
        }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马