从键盘输入以上数据(包括姓名,三门课成绩),
输入的格式:如:zhagnsan,30,40,60计算出总成绩,
并把学生的信息和计算出的总分数高低顺序存放在磁盘文件"stud.txt"中。
1:定义一个描述学生的类
2定义一个操作学生的工具类.0
//比较棒的程序,用到了流读写文件,集合泛型
import java.util.*;
class Student implements Comparable<Student>
{
private String name;
private int shu,yu,eng;
private int sum;
public Student(){}
public Student(String name,int shu,int yu,int eng)
{
this.name = name;
this.shu = shu;
this.yu = yu;
this.eng = eng;
sum = shu+yu+eng;
}
public int compareTo(Student stu)
{
int num = this.sum-stu.sum;
return num==0?this.name.compareTo(stu.name):num;
}
public String getName()
{
return name;
}
public int getShu()
{
return shu;
}
public int getYu()
{
return yu;
}
public int getEng()
{
return eng;
}
public int getSum()
{
return sum;
}
public String toString()
{
return name+","+shu+","+yu+","+eng;
}
}
class StuTool
{
public static Set<Student> getStudents() throws IOException
{
return getStudents(null);
}
//从键盘获取学员信息,存到集合中
public static Set<Student> getStudents(Comparator<Student> com)throws IOException
{
TreeSet<Student> ts = null;
if(com==null)
ts = new TreeSet<>();
else
ts = new TreeSet<>(com);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
for(int i=1;i<=3;i++)
{
System.out.println("请输入第"+i+"个学员的信息");
line = br.readLine();
String[] arr = line.split(",");
ts.add(new Student(arr[0],Integer.parseInt(arr[1]),Integer.parseInt(arr[2]),Integer.parseInt(arr[3])));
}
br.close();
return ts;
}
//把学员信息 按照成绩从高到低写入到文件中
public static void writeInfo(Set<Student> stus)throws IOException
{
BufferedWriter bw = new BufferedWriter(new FileWriter("stud.txt"));
for(Student stu:stus)
{
bw.write(stu.toString()+" ");
bw.write(""+stu.getSum());
bw.newLine();
bw.flush();
}
bw.close();
}
}
class n1
{
public static void main(String[] args) throws IOException
{
Comparator<Student> com = Collections.reverseOrder();
Set<Student> stus = StuTool.getStudents(com);
//但是这一句采用由低到高的默认顺序
// Set<Student> stus = StuTool.getStudents();
StuTool.writeInfo(stus);
}
}
//采用更简明的解法
import java.io.*;
import java.util.*;
class Student {
private String name;
private int chinese;
private int math;
private int english;
private int sum;
public Student(){}
public Student(String name, int chinese, int math, int english){
super();
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
this.sum = this.chinese + this.math + this.english;
}
public int getSum(){
return sum;
}
public String toString(){
return name+","+chinese+","+math+","+english+",总成绩:"+sum;
}
}
class ComBySum implements Comparator<Student>
{
public int compare(Student t1, Student t2)
{
int num=t1.getSum()-t2.getSum();
return num;
}
}
public class n1 {
public static void main(String[] args) throws IOException
{
getMassage();
}
public static void getMassage() throws IOException{
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生信息,输入格式为:name,30,30,30");
ComBySum comBySum=new ComBySum();
TreeSet<Student> ts = new TreeSet<>(comBySum);
for(int i=0;i<5;i++)
{
String line = sc.nextLine();
String[] arr;
int chinese;
int math;
int english;
try {
arr = line.split("\\,");
chinese = Integer.parseInt(arr[1]);
math = Integer.parseInt(arr[2]);
english = Integer.parseInt(arr[3]);
ts.add(new Student(arr[0],chinese,math,english));
} catch (NumberFormatException e) {
System.out.println("录入格式错误,请重新录入。。。。");
}
}
BufferedWriter bw = new BufferedWriter(new FileWriter("stu.txt"));
for(Student s : ts)
{
bw.write(s.toString());
bw.newLine();
}
bw.close();
}
}