Advertisement
LarvitarYoung

Q1

Jan 13th, 2021
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. package smallreview;
  2. import java.util.*;
  3. import java.io.*;
  4. class Score implements Comparable<Score>{
  5. private int id;
  6. private String name;
  7. private float cscore;
  8. private float escore;
  9. private float mscore;
  10. private float sscore;
  11. private float bscore;
  12. private float total;
  13. private float avg;
  14. Score(int id,String name,float cscore,float escore,float mscore,float sscore,float bscore){
  15. this.id=id;
  16. this.name=name;
  17. this.cscore=cscore;
  18. this.escore=escore;
  19. this.mscore=mscore;
  20. this.sscore=sscore;
  21. this.bscore=bscore;
  22. this.total=cscore+escore+mscore+sscore+bscore;
  23. this.avg=total/5;
  24. }
  25. @Override
  26. public String toString() {
  27. return this.id+"\t"+this.name+"\t"+this.cscore+"\t"+this.escore+"\t"+this.mscore+"\t"+this.sscore+"\t"+this.bscore+"\t"+this.total+"\t"+this.avg;
  28. }
  29. @Override
  30. public int compareTo(Score o) {
  31. return this.cscore==o.cscore?(this.escore>o.escore?-1:1):(this.cscore>o.cscore?-1:1);
  32. }
  33. }
  34. public class file {
  35. static void insert(List<Float> list ,Scanner sc) {
  36. String[] temp;
  37. while(sc.hasNext()) {
  38. temp=sc.nextLine().split(",");
  39. list.add(Float.parseFloat(temp[1]));
  40. }
  41. }
  42. public static void main(String[] args) throws FileNotFoundException {
  43. List<String>nm=new ArrayList<>();
  44. List<Float> cs=new ArrayList<>();
  45. List<Float> es=new ArrayList<>();
  46. List<Float> ms=new ArrayList<>();
  47. List<Float> ss=new ArrayList<>();
  48. List<Float> bs=new ArrayList<>();
  49. List<Integer> idx=new ArrayList<>();
  50. // math
  51. File f = new File("C:\\Users\\USER\\Desktop\\mid1\\mid1\\math.txt");
  52. Scanner sc = new Scanner(f);
  53. insert(ms,sc);
  54. // cs
  55. f=new File("C:\\Users\\USER\\Desktop\\mid1\\mid1\\chinese.txt");
  56. sc = new Scanner(f);
  57. insert(cs,sc);
  58. //es
  59. f=new File("C:\\Users\\USER\\Desktop\\mid1\\mid1\\english.txt");
  60. sc = new Scanner(f);
  61. insert(es,sc);
  62. //ss
  63. f=new File("C:\\Users\\USER\\Desktop\\mid1\\mid1\\society.txt");
  64. sc = new Scanner(f);
  65. insert(ss,sc);
  66. //bs
  67. f=new File("C:\\Users\\USER\\Desktop\\mid1\\mid1\\biological.txt");
  68. sc = new Scanner(f);
  69. insert(bs,sc);
  70. //name
  71. f=new File("C:\\Users\\USER\\Desktop\\mid1\\mid1\\name.txt");
  72. sc = new Scanner(f);
  73. String[] temp;
  74. while(sc.hasNext()) {
  75. temp=sc.nextLine().split(",");
  76. idx.add(Integer.parseInt(temp[0]));
  77. nm.add(temp[1]);
  78. }
  79. List<Score> list = new ArrayList<>();
  80. System.out.println("座號\t姓名\t國文\t英文\t數學\t社會\t生物\t總分\t平均");
  81. for(int i=0;i<idx.size();i++) {
  82. list.add(new Score(idx.get(i),nm.get(i),cs.get(i),es.get(i),ms.get(i),ss.get(i),bs.get(i)));
  83. }
  84. Collections.sort(list);
  85. for(Score e :list) {
  86. System.out.println(e);
  87. }
  88. }
  89.  
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement