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();
}