标签:相同 opened void read private 键盘 写入文件 tor click
1.先写一个Student类
public class Student { private String name; private int chinese; private int math; private int english; public Student() { super(); } public Student(String name, int chinese, int math, int english) { 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) { this.chinese = chinese; } public int getMath() { return math; } public void setMath(int math) { this.math = math; } public int getEnglish() { return english; } public void setEnglish(int english) { this.english = english; } public int getTotal(){ return (this.chinese + this.math + this.english); } @Override public String toString() { return "Student{" + "name=‘" + name + ‘\‘‘ + ", chinese=" + chinese + ", math=" + math + ", english=" + english + ‘}‘; } }
2.录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.Comparator; import java.util.TreeSet; public class Demo { public static void main(String[] args) throws IOException { //创建5个学生对象 Student s1 = new Student("孙悟空",80,80,80); Student s2 = new Student("猪八戒",90,90,90); Student s3 = new Student("玉皇大帝",100,100,100); Student s4 = new Student("嫦娥",100,100,100); Student s5 = new Student("白骨精",90,80,100); //按照总分从高到低存入TreeSet TreeSet<Student> set = new TreeSet<>(new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { int cmp1 = s2.getTotal() - s1.getTotal();//总分从高到低排序 int cmp2 = cmp1 == 0 ? s2.getName().compareTo(s1.getName()) : cmp1;//保证可以出现总分相同但是名字不同的学生 return cmp2; } }); //将学生信息存入集合 set.add(s1); set.add(s2); set.add(s3); set.add(s4); set.add(s5); //3.遍历集合并写入文件 BufferedWriter writer = new BufferedWriter(new FileWriter("Day27_Thread01\\student.txt")); for (Student s :set) { StringBuilder sb = new StringBuilder("姓名:" + s.getName() + ", 语文成绩:" + s.getChinese() + ", 数学成绩:" + s.getMath() + ", 英语成绩:" + s.getEnglish()); writer.write(sb.toString()); writer.newLine(); writer.flush(); } writer.close(); } }
3.键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.Comparator; import java.util.Scanner; import java.util.TreeSet; public class Demo2 { public static void main(String[] args) throws IOException { //创建TreeSet集合 TreeSet<Student> set = new TreeSet<>(new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { int cmp1 = s2.getTotal() - s1.getTotal(); int cmp2 = cmp1 == 0 ? s2.getName().compareTo(s1.getName()) : cmp1; return cmp2; } }); for (int i = 0;i < 5;i++){ //输入学生信息 Scanner sc = new Scanner(System.in); System.out.println("请输入学生的姓名:"); String name = sc.nextLine(); System.out.println("请输入该学生的语文成绩:"); int chinese = sc.nextInt(); System.out.println("请输入该学生的数学成绩:"); int math = sc.nextInt(); System.out.println("请输入该学生的英语成绩:"); int english = sc.nextInt(); //创建学生对象并录入信息 Student s = new Student(); s.setName(name); s.setChinese(chinese); s.setMath(math); s.setEnglish(english); //将学生添加到集合里 set.add(s); } //3.遍历集合并写入文件 BufferedWriter writer = new BufferedWriter(new FileWriter("Day27_Thread01\\student.txt")); for (Student s :set) { StringBuilder sb = new StringBuilder("姓名:" + s.getName() + ", 语文成绩:" + s.getChinese() + ", 数学成绩:" + s.getMath() + ", 英语成绩:" + s.getEnglish()); writer.write(sb.toString()); writer.newLine(); writer.flush(); } //关闭资源 writer.close(); } }
键盘录入学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
标签:相同 opened void read private 键盘 写入文件 tor click
原文地址:https://www.cnblogs.com/bug-baba/p/10558050.html