1-10练习

1、保存5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
package com.qiku.homework_1_11;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortStudentInfo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        List<Student> students = new ArrayList<>();
        stuudents.add(new Student11("张三", 99, 86, 80));
        stuudents.add(new Student11("李四", 98, 90, 74));
        stuudents.add(new Student11("王五", 68, 70, 78));
        stuudents.add(new Student11("赵六", 75, 86, 72));
        stuudents.add(new Student11("钱七", 88, 89, 95));
        System.out.println(students);
        Collections.sort(students);
        System.out.println(students);

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


        FileInputStream fis = new FileInputStream("file\\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();



    }
}

 student类

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 static void test1() {

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    for (int i = 1; i < 101; i++) {
                        System.out.println(i);
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }

            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                Scanner scan = new Scanner(System.in);
                String str = scan.next();
                while (!str.equals("Q")) {
                    System.out.println("请输入您的指令:9");
                }
            }
        });
        t1.setDaemon(true);
        t1.start();
        t2.start();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值