[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;
}
}
[Java] 纯文本查看 复制代码
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.Scanner;
import java.util.TreeMap;
import com.bean.Student;
public class Practice1 {
public static void main(String[] args) {
//創建一個鍵盤輸入對象input
Scanner input = new Scanner(System.in);
//创建一个学生数组,接收学生信息
Student[] students = new Student[5];
File file = new File("stu.txt");
//计数器,判断输入学生数量
int count = 0;
//创建一个匿名内部对象,Comparator并重写compare方法
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 < students.length) {
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");
bWriter.flush();
bWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
刚把里面的注释加了一下,有几个地方做了修正,还请各位大神指正!