所以我写的程序会读取3个不同的文本文件。一个文本文件包含名称,另外两个包含标记。
现在我已经正确地完成了所有事情,但是我还想补充一点,但是我没有运气。
所以现在输出文件如下所示:
25987 Alan
IR101: 35.6 IR102: 20.7 Aggregate: 28.2
Class: Fail Outcome: Repeat Year!
-------------------------------------------------------
25954 Betty
IR101: 70.2 IR102: 63.4 Aggregate: 66.8
Class: 2.1 Outcome: Proceed to Stage 2!
-------------------------------------------------------
25654 Chris
IR101: 58.6 IR102: 35.1 Aggregate: 46.9
Class: Fail Outcome: Resit IR102!
-------------------------------------------------------
Etc
因此,我的程序会根据名称从文本文件中打印出名称。例如,其中一个文本文件中包含所有名称的顺序是:Alan / n Betty / n Chris
现在我不希望订单来自文本文件中的名称我希望订单是降序聚合标记。所以订单应该是:
25954 Betty
IR101: 70.2 IR102: 63.4 Aggregate: 66.8
Class: 2.1 Outcome: Proceed to Stage 2!
-------------------------------------------------------
25654 Chris
IR101: 58.6 IR102: 35.1 Aggregate: 46.9
Class: Fail Outcome: Resit IR102!
-------------------------------------------------------
25987 Alan
IR101: 35.6 IR102: 20.7 Aggregate: 28.2
Class: Fail Outcome: Repeat Year!
-------------------------------------------------------
我已经尝试了很多不同的解决方案,但它们都失败了。
程序代码:
public class SORTING {
static class Student {
String id;
String name;
List<Double> marks;
public Student(String id, String name) {
this.id = id;
this.name = name;
marks = new ArrayList<Double>();
}
public void addMark(Double d) {
marks.add(d);
}
public void writeToPW(PrintWriter out) {
out.println(id + " " + name);
double d = 0;
for (int i = 0; i < marks.size(); i++) {
out.printf("IR10%d: %.1f ", (i+1), marks.get(i));
d += marks.get(i);
}
out.printf("Aggregate: %.1f ", + d / marks.size());
out.println("\n");
double aggregate = (d/marks.size());
if ((marks.get(0)<40)&&(marks.get(1)>=40)){
out.print("Class: Fail" + " Outcome: Resit IR101");
}
if ((marks.get(0)>=40)&&(marks.get(1)<40)){
out.print("Class: Fail" + " Outcome: Resit IR102!");
}
if ((marks.get(0)<40)&&(marks.get(1)<40)){
out.print("Class: Fail" + " Outcome: Repeat Year!");
}
if((marks.get(0)>40)&&(marks.get(1)>40)&&(aggregate>70)){
out.print("Class: 1st" + " Outcome: Proceed to Stage 2!");
}
if((marks.get(0)>40)&&(marks.get(1)>40)&&(aggregate>=60)&&(aggregate<=69.9)){
out.print("Class: 2.1" + " Outcome: Proceed to Stage 2!");
}
//2.2 Class degree code.
if((marks.get(0)>40)&&(marks.get(1)>40)&&(aggregate>=50)&&(aggregate<=59.9)){
out.print("Class: 2.2" + " Outcome: Proceed to Stage 2!");
}
if((marks.get(0)>40)&&(marks.get(1)>40)&&(aggregate>=40)&&(aggregate<=49.9)){
out.print("Class: 3rd" + " Outcome: Proceed to Stage 2!");
}
out.println("\n");
out.println("-------------------------------------------------------");
}
}
public static void main(String[] args) throws IOException {
//declare reader and writer
BufferedReader reader = null;
PrintWriter writer = null;
//hash maps to store the data
HashMap<String, Student> students = new HashMap<String, Student>();
// list to maintain the original order
List<Student> orderedStudents = new ArrayList<Student>();
//read the first file and store the data
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IRStudents.txt"))));
String line;
String[] arg;
while ((line = reader.readLine()) != null) {
if (!line.startsWith("-")) {
arg = line.split(" ");
Student student = new Student(arg[0], arg[1]);
students.put(arg[0], student);
orderedStudents.add(student);
}
}
reader.close();
//read the second file, merge the data and output the data to the out file
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IR101.txt"))));
while((line = reader.readLine()) != null){
arg = line.split(" ");
students.get(arg[0]).addMark(Double.parseDouble(arg[1]));
}
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IR102.txt"))));
while((line = reader.readLine()) != null){
arg = line.split(" ");
students.get(arg[0]).addMark(Double.parseDouble(arg[1]));
}
// Now we can do writing.
writer = new PrintWriter(new FileOutputStream(new File("RankedList.txt")));
for (Student s: orderedStudents) {
s.writeToPW(writer);
}
writer.close();
}
}
答案 0 :(得分:1)
好吧,您需要使用https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-java.util.Comparator-对列表进行排序。你给它一个比较器,它是Comparator接口的一个实现,基本上取两个Student对象并返回它们应该如何排序,在你的情况下通过比较聚合标记。 像
这样的东西Collections.sort(orderedStudents, new Comparator<Student>() {
public int compare(Student s1,Student s2){
return s2.getAggregate().compareTo(s1.getAggregate());
}
});
并在Student类上公开实际计算聚合的getAggregate()方法,而不是在漂亮的打印代码中进行计算。