【无标题】

  1、保存5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件 
public class SortStudentInfo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
       
        List<Student> students = new ArrayList<>();
        students.add(new Student("zs1" , 23,64,63));
        students.add(new Student("zs2" , 56,23,56));
        students.add(new Student("zs3" , 43,87,68));
        students.add(new Student("zs4" , 87,34,83));
        students.add(new Student("zs5" , 12,65,53));
        System.out.println(students);

//        Collections.sort(students, new Comparator<Student>() {
//            @Override
//            public int compare(Student stu1, Student stu2) {
//                double num = stu1.getSumScore() - stu2.getSumScore();
//                if (num == 0){ // 总分相同  按名字排序
//                    return stu1.getName().compareTo(stu2.getName()) ;//
//                }else {
//                    return num > 0 ?  1 :  -1;
//                }
//            }
//        });
        Collections.sort(students);
        System.out.println(students);

        FileOutputStream fos = new FileOutputStream("testIO\\students.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(students);

        System.out.println("----------");

        FileInputStream fis = new FileInputStream("testIO\\students.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);

        Object o = ois.readObject();

        if (o instanceof List){
            List list = (List) o;
            System.out.println(list);
        }

        oos.close();
        fos.close();
        ois.close();
        fis.close();
    }
}

import java.io.Serializable;

public class Student implements Comparable<Student> , Serializable {
    private String name;
    private double chScore;
    private double mathScore;
    private double engScore;


    public Student() {
    }

    public Student(String name, double chScore, double mathScore, double engScore) {
        this.name = name;
        this.chScore = chScore;
        this.mathScore = mathScore;
        this.engScore = engScore;
    }


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", chScore=" + chScore +
                ", mathScore=" + mathScore +
                ", engScore=" + engScore +
                ", 总分=" + getSumScore() +
                '}';
    }

    public double getSumScore() {
        return chScore + mathScore + engScore;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getChScore() {
        return chScore;
    }

    public void setChScore(double chScore) {
        this.chScore = chScore;
    }

    public double getMathScore() {
        return mathScore;
    }

    public void setMathScore(double mathScore) {
        this.mathScore = mathScore;
    }

    public double getEngScore() {
        return engScore;
    }

    public void setEngScore(double engScore) {
        this.engScore = engScore;
    }


    @Override
    public int compareTo(Student stu) {
        double num = this.getSumScore() - stu.getSumScore();
        if (num == 0) { // 总分相同  按名字排序
            return this.getName().compareTo(stu.getName());//
        } else {
            return num > 0 ? 1 : -1;
        }
    }
}

 2、打印数字,读取通知 (1)在main方法中启动两个线程; (2)在1个线程循环打印100以内的整数; (3)直到第2个线程从键盘读取了“Q”命令。 

public class PrintNumReadMsg {
    public static void main(String[] args) {
      

        PrintNumThread pnt = new PrintNumThread();
        ReadMsgThread rmt = new ReadMsgThread(pnt);

        Thread tp = new Thread(pnt);
        Thread tr = new Thread(rmt);

//        tp.setDaemon(true);

        tp.start();
        tr.start();



    }
}

/**
 * 打印数字的线程
 */
public class PrintNumThread implements Runnable{

    boolean loop = true;

    @Override
    public void run() {
        while (loop){
            try {
                Thread.sleep(100);
                System.out.println((int)(Math.random() * 100));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}

/**
 * 监听键盘输入的信息
 */
public class ReadMsgThread implements Runnable{

    Scanner sc = new Scanner(System.in);
    // 将  打印数字的线程的对象   当做 监听信息的  成员变量
    private PrintNumThread printNumThread;

    public ReadMsgThread(PrintNumThread printNumThread) {
        this.printNumThread = printNumThread;
    }

    @Override
    public void run() {
        while (true){
            System.out.println("请输入您的指令:");
            String input = sc.next();
            if (input.equals("q")){
                printNumThread.loop = false;
                break;
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值