[Java] 纯文本查看 复制代码 /*有五个学生,每个学生有3门课(语文、数学、英语)的成绩,写一个程序接收从键盘输入学生的信息,输入格式为:name,30,30,30(姓名,三门课成绩),
然后把输入的学生信息按总分从高到低的顺序写入到一个名称"stu.txt"文件中。
要求:stu.txt文件的格式要比较直观,打开这个文件,就可以很清楚的看到学生的信息。
分析:1、创建一个学生类,包含如下信息
成员变量:name 语文、数学、英语成绩
构造方法:空参,有参,包含总成绩
成员方法:获取总成绩的方法 重写toString方法
2、創建一個鍵盤輸入對象,創建字符串,接收輸入的內容,格式為:name,30,30,30(姓名,三门课成绩)
3、創建一個5次的循环,分别接收5个字符串,
4、创建一个字符串数组,将接收的字符串用用string.split将字符串分成四部分
5、根据数组元素创建学生对象
6、因为按照总分高低排序,所以创建一个treeMap(student,sum)集合
7、遍历集合,将集合中信息打印出来*/
package com.demo;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeMap;
import com.bean.Student;
public class Practice1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Student[] students = new Student[5];
File file = new File("stu.txt");
int count = 0;
TreeMap<Student, Integer> tm = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s2.getSumScore() - s1.getSumScore();
return num == 0 ? s2.getName().compareTo(s1.getName()) : num;
}
});
while (true) {
if (count < 3) {
try {
System.out.println("请输入一个学生信息,格式为name,30,30,30(姓名,三门课成绩):");
String[] string = input.nextLine().split(",");
int chineseScore = Integer.parseInt(string[1]);
int mathScore = Integer.parseInt(string[2]);
int englishbScore = Integer.parseInt(string[3]);
students[count] = new Student(string[0], chineseScore,mathScore,englishbScore);
tm.put(students[count], students[count].getSumScore());
count++;
} catch (ArrayIndexOutOfBoundsException aiobe) {
// TODO: handle exception
System.out.println("格式输入错误,请输入!");
} catch (NumberFormatException nfe) {
// TODO: handle exception
System.out.println("格式输入错误,你输入了非整数字符!");
} catch (Exception e) {
// TODO: handle exception
System.out.println("格式输入错误!");
}
}else {
break;
}
}
//遍历集合
try {
BufferedWriter bWriter = new BufferedWriter(new FileWriter(file));
for (Student student : tm.keySet()) {
bWriter.write(student.toString() + ",SumScore:" + student.getSumScore() + "\n");;
//System.out.print(student);
//System.out.println(",sumScore" + student.getSumScore());
}
bWriter.flush();
bWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.bean;
/**
* @author jack
*
*/
public class Student {
private String name;
private int chineseScore;
private int mathScore;
private int englishScore;
private int sumScore;
public Student() {
super();
}
public Student(String name, int chineseScore, int mathScore, int englishScore) {
super();
this.name = name;
this.chineseScore = chineseScore;
this.mathScore = mathScore;
this.englishScore = englishScore;
this.sumScore = this.chineseScore + this.englishScore + this.mathScore;
}
public String getName() {
return name;
}
public int getSumScore() {
return sumScore;
}
@Override
public String toString() {
return "name:" + name + ", chineseScore:" + chineseScore + ", mathScore:" + mathScore
+ ", englishScore:" + englishScore;
}
}
刚写完的,测试可以用,代码的结构和简洁性没有考虑,注释没有添加,交流学习! |