鍵盤錄入學生信息(姓名,語文成績,數學成績,英語成績),按照總分從高到低存入文本文件
阿新 • • 發佈:2019-03-19
pen col 數學 clas scanner closed ext div for
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;Studentthis.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(intenglish) { 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(); } }Demo
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(); } }Demo2
鍵盤錄入學生信息(姓名,語文成績,數學成績,英語成績),按照總分從高到低存入文本文件