所以我正在编写的程序读取 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();
}
}