1、保存5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
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;
}
}
}
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 {
/**
* 1、保存5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
*/
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();
}
}
2、打印数字,读取通知
(1)在main方法中启动两个线程;
(2)在1个线程循环打印100以内的整数;
(3)直到第2个线程从键盘读取了“Q”命令。
/**
* 打印数字的线程
*/
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();
}
}
}
}
import java.util.Scanner;
/**
* 监听键盘输入的信息
*/
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;
}
}
}
}
import java.util.Queue;
public class PrintNumReadMsg {
public static void main(String[] args) {
/**
2、打印数字,读取通知
(1)在main方法中启动两个线程;
(2)在1个线程循环打印100以内的整数;
(3)直到第2个线程从键盘读取了“Q”命令。
*/
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();
}
}