package com.shujia.day17.ketang;
/*
键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
*/
import java.io.BufferedReader;
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 IOTest3 {
public static void main(String[] args) throws IOException {
//创建键盘录入对象
Scanner sc = new Scanner(System.in);
//创建TreeSet集合对象
TreeSet<Student> set = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
//主要条件:总分从高到低
int i = o2.getSumScore() - o1.getSumScore();
//隐含条件
//总成绩一样,语文成绩不一定一样
int i2 = (i == 0) ? o2.getChinese() - o1.getChinese() : i;
//语文成绩一样,数学成绩不一定一样
int i3 = (i2 == 0) ? o2.getMath() - o1.getMath() : i2;
//姓名不一定一样
int i4 = (i3 == 0) ? o2.getName().compareTo(o1.getName()) : i3;
return i4;
}
});
Student student = null;
for (int i = 1; i <= 5; i++) {
System.out.println("请输入第" + i + "个学生的信息:");
System.out.println("请输入姓名:");
String name = sc.next();
System.out.println("请输入语文成绩:");
int chinese = sc.nextInt();
System.out.println("请输入数学成绩:");
int math = sc.nextInt();
System.out.println("请输入英语成绩:");
int english = sc.nextInt();
student = new Student(name, chinese, math, english);
set.add(student);
System.out.println("---------------------------------------------");
}
StringBuffer sb = null;
//创建字符缓冲输入流对象
BufferedWriter bw = new BufferedWriter(new FileWriter("studentInfo.txt"));
//遍历集合得到每个学生信息,将学生信息组成字符串写入到文件中
for(Student student1 : set){
sb = new StringBuffer();
String name = student1.getName();
int chinese = student1.getChinese();
int math = student1.getMath();
int english = student1.getEnglish();
int sumScore = student1.getSumScore();
sb.append("姓名:")
.append(name)
.append(",语文成绩:")
.append(chinese)
.append(",数学成绩:")
.append(math)
.append(",英语成绩:")
.append(english)
.append(",总成绩:")
.append(sumScore);
//将StringBuffer转成字符串
String s = sb.toString();
bw.write(s);
bw.newLine();
bw.flush();
}
System.out.println("所有学生信息录入存储完毕!");
//释放资源
bw.close();
}
}
student类
package com.shujia.day17.ketang;
public class Student {
private String name;
private int chinese;
private int math;
private int english;
public Student() {
}
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 getSumScore() {
return chinese + math + english;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", chinese=" + chinese +
", math=" + math +
", english=" + english +
'}';
}
}
标签:name,chinese,int,IO,english,集合,public,math
From: https://www.cnblogs.com/qiwei-bigdata/p/18365017