OOP三次大作業總結-BLOG-3
面向對象程序設計三次大作業總結
一、前言。
7-1 課程成績統計程序-1
該題涉及的Java知識點包括輸入輸出、字符串處理、數據結構和算法、異常處理等。
題目要求解析輸入的課程信息和成績信息,并根據規定的計算方式計算學生的總成績和平均分,以及各門課程的平均分和班級的總成績平均分。同時需要處理各種異常情況。
題量較大,涉及多個輸入輸出項和計算要求。
難度適中,需要對輸入輸出進行合理的處理,同時需要注意各種異常情況的處理。需要對字符串處理和數據結構算法有一定的掌握。
在解決該題目時,可以考慮以下方面:
1. 輸入輸出處理:首先需要讀取輸入的課程信息和成績信息,并將其解析為合適的數據結構進行存儲和處理。同時,需要注意對輸入格式的合法性進行校驗,并對異常情況進行適當的處理。
2. 數據結構和算法:可以使用合適的數據結構來存儲課程信息和成績信息,例如使用HashMap來存儲課程名稱和對應的成績列表,或使用對象來存儲每個學生和對應的課程成績信息。在計算總成績和平均分時,可以使用循環遍歷數據結構,并按照題目要求進行計算。
3. 異常處理:需要考慮各種異常情況的處理,例如輸入格式錯誤、重復的課程或成績信息、課程性質和考核方式不匹配等。對于這些異常情況,可以通過合適的異常處理機制進行捕獲和處理,并輸出對應的錯誤信息。
4. 計算總成績和平均分:根據題目要求,需要根據課程的性質和考核方式來計算總成績和平均分。對于必修課,采用考試方式計算總成績;對于選修課,可以根據考試或考察方式來計算總成績。在計算平均分時,需要進行加權平均。
5. 排序輸出:根據題目要求,需要對學生和班級的成績進行排序輸出。可以使用排序算法對學生或班級的成績進行排序,然后按照題目要求格式進行輸出。
綜上所述,該題目需要綜合運用Java的輸入輸出、字符串處理、數據結構和算法、異常處理等知識點。對于這些知識點的熟練掌握和靈活運用,將有助于解決該題目。
7-3 課程成績統計程序-2
該題涉及的Java知識點包括輸入輸出、字符串處理、數據結構和算法、異常處理等。相比上一版本,本題增加了處理實驗課的內容,需要對實驗成績進行計算。
題量適中,涉及多個輸入輸出項和計算要求。
難度中等偏上,需要綜合運用Java的輸入輸出、字符串處理、數據結構和算法、異常處理等知識點。另外,需要考慮對實驗成績的處理,并進行對應的計算。同時,還需注意各種異常情況的處理和輸出結果的格式要求。對于這些知識點的熟練掌握和靈活運用,將有助于解決該題目。
在解決該題目時,可以考慮以下方面進行完善:
1. 進一步優化數據結構:可以使用合適的數據結構來存儲課程信息和成績信息,例如使用HashMap或者ArrayList來存儲課程名稱和對應的成績列表,或者使用對象來存儲每個學生和對應的課程成績信息。可以根據需求進行選擇,以便在后續的計算和輸出中更高效地處理數據。
2. 實驗課成績的計算:由于新增了實驗課程,需要對實驗成績進行計算。可以在數據結構中增加相應的字段來存儲實驗次數和每次實驗成績,并在計算總成績時考慮實驗成績的權重和計算方式。
3. 高效處理重復的課程/成績信息:當輸入中出現重復的課程或成績信息時,可以通過判斷是否已經存在相同課程名稱或學生信息來避免重復處理。可以使用HashSet或其他數據結構來記錄已經處理過的課程信息,以提高程序的效率。
4. 考慮代碼的可讀性和可維護性:根據題目要求,可以合理拆分代碼邏輯為多個方法,每個方法負責處理一個特定的功能,便于代碼的組織和維護。還可以添加適當的注釋和命名規范,提高代碼的可讀性和可維護性。
5. 提高程序的健壯性:需要對輸入進行充分的合法性檢查,并對異常情況進行處理。可以使用正則表達式對輸入進行匹配,以確保輸入的格式正確,并且需要進行各種邊界值的判斷,避免超出范圍的錯誤。
6. 進一步優化算法:在計算總成績和平均分時,可以考慮使用累加的方式來計算,在遍歷數據結構時累加各門課程成績,這樣可以減少不必要的循環和計算。
綜上所述,該題目需要綜合運用Java的輸入輸出、字符串處理、數據結構和算法、異常處理等知識點,對于這些知識點的熟練掌握和靈活運用,將有助于解決該題目。在代碼的邏輯結構、代碼風格和異常處理等方面進行優化,可以提高程序的可讀性、可維護性和健壯性。
7-2 課程成績統計程序-3
該題涉及的Java知識點包括輸入輸出、字符串處理、數據結構和算法、異常處理等。相比上兩個版本,本題修改了成績類的繼承關系為組合關系,并增加了分項成績類來進行成績的計算。
題量適中,涉及多個輸入輸出項和計算要求,同時還有對繼承和組合關系的理解和比較。
難度中等,需要綜合運用Java的輸入輸出、字符串處理、數據結構和算法、異常處理等知識點來解決問題。此外,還需要理解并比較繼承和組合關系的區別,思考哪種關系更適應變更,并相應地修改代碼。
通過修改類結構、采用組合關系的方式,可以使代碼更加靈活,模塊化程度更高。通過使用分項成績類,可以根據權重計算各個分項的成績,并在計算總成績時進行累加。這樣可以更容易適應需求的變更,例如增加新的分項成績或修改權重,而無需修改大量的代碼。
因此,通過使用組合關系來構建成績類,代碼更具靈活性和可維護性,能夠更好地適應變更。對于這些知識點的熟練掌握和靈活運用,將有助于解決該題目,并能更好地理解和應用繼承和組合關系。
在解決課程成績統計程序-3的問題時,可以進一步考慮以下方面:
1. 組合關系的優勢:與繼承關系相比,組合關系更具靈活性和可擴展性。通過將分項成績類與課程成績類進行組合,可以輕松地處理單門課程的多個成績分項,并根據權重來計算總成績。這種設計模式符合“組合優于繼承”的原則,能夠更好地適應需求變更和擴展。
2. 可配置的分項成績權重:由于新增了分項成績類,在錄入課程信息時,需要輸入每個分項成績的權重。這樣的設計允許靈活地對分項成績進行權重分配,可以根據實際情況進行設置。例如,對于某門課程,可以根據教學目標和考核要求來確定各個分項成績的比重,從而更準確地計算總成績。
3. 總成績計算的實現:在計算總成績時,通過分項成績類的權重和成績來計算各個分項的得分,然后將得分相加得到總成績。這種分項成績和總成績的計算方式更加靈活,可以適應不同課程的考核方式和評分標準。
4. 異常處理的完善:需要對異常情況進行處理。例如,檢查輸入的分項成績數量和權重的數量是否匹配,檢查分項成績權重的總和是否為1,以及對其他格式錯誤和越界的輸入進行處理。通過合理的異常處理機制,可以更好地保證程序的穩定性和可靠性。
總的來說,課程成績統計程序-3在前兩個版本的基礎上,通過改變成績類的繼承關系為組合關系,并引入了分項成績類來處理分項成績和計算總成績。通過這種設計方式,代碼變得更靈活、可擴展性更強,能夠更好地適應需求的變更。對于這些知識點的理解和靈活運用,將有助于解決該問題,并在實際應用中具備更好的適應性和可維護性。
二、設計與分析。
對PTA中成績計算系列題目的設計與分析。
課程成績統計程序-1
1)類圖

2)分析
該代碼是一個學生成績管理系統,能夠進行課程信息的輸入、學生成績的錄入和統計分析。主要流程如下:
1. 首先,通過鍵盤輸入獲取課程和學生成績的輸入數據。
2. 輸入數據以換行符分割成不同的行,然后分別處理每一行的數據。
3. 對于輸入的每一行數據,使用正則表達式進行匹配,以確定輸入類型。
4. 如果輸入是課程信息,則根據輸入的課程名稱、性質和考核方式創建課程對象,并根據輸入信息設置相應的屬性。
5. 如果輸入是學生成績信息,則根據輸入的學生學號、姓名和課程名稱,創建學生對象和成績對象,并根據輸入信息設置相應的屬性。如果該成績信息對應的課程不存在,則輸出錯誤提示信息。
6. 每錄入一個學生的成績,將成績信息添加到相應學生的成績列表中。
7. 在學生成績錄入完畢后,遍歷學生列表,輸出每個學生的學號、姓名和最終成績。
8. 遍歷課程列表,輸出每門課程的名稱、平時成績、考試成績和最終成績。
9. 遍歷班級列表,輸出每個班級的班級號和平均成績。
10. 最后,程序輸出是否需要存儲所有課程的信息的提示,如果需要,則創建一個存儲課程信息的Map,然后通過鍵盤輸入獲取課程信息,并存儲到Map中。
根據代碼分析,總結該系統的功能如下:
- 可以錄入課程信息,包括課程名稱、性質和考核方式。
- 可以錄入學生成績信息,包括學生學號、姓名、課程名稱和成績。
- 統計并輸出每個學生的最終成績。
- 統計并輸出每門課程的平時成績、考試成績和最終成績。
- 統計并輸出每個班級的平均成績。
- 可以選擇是否存儲所有課程的信息,并提供相應的提示。
該代碼使用了面向對象的思想,將課程、學生和班級的信息封裝成了類,并通過對象的屬性和方法進行操作和管理。同時,代碼使用了正則表達式來匹配輸入的內容,確保輸入的數據格式正確。
需要注意的是,該代碼中存在一些問題和改進空間。例如,Course類中的變量和方法應該使用private修飾符進行封裝,避免直接訪問和修改,可以增加相應的get和set方法。還有一些邏輯判斷和錯誤處理的代碼可以進一步優化。
3)源碼
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Course[] course = new Course[100]; Student[] student = new Student[100]; Cls[] cls = new Cls[100]; int courseNumber = 0; //課程數量 int studentNumber = 0; //學生數量 int classCount = 0; //班級數量 StringBuilder t = new StringBuilder(); while (sc.hasNextLine()) { String s = sc.nextLine(); if (s.equals("end")) { break; } t.append(s).append("\n"); } String str = t.toString(); String[] lines = str.split("\n"); for(String line : lines) { String[] words = line.split(" "); if(InputMatching.matchingInput(line)==1) { course[courseNumber] = new Course(); course[courseNumber].setName(words[0]); if(words[1].equals("必修")) course[courseNumber].required = true; else if(words[1].equals("選修")) course[courseNumber].required = false; if(course[courseNumber].required) { course[courseNumber].test = true; if(words[2].equals("考察")){ course[courseNumber].format = false; System.out.println(words[0]+" : course type & access mode mismatch"); course[courseNumber].setName(null); } }else{ if(words[2].equals("考試")){ course[courseNumber].test = true; }else if(words[2].equals("考察")){ course[courseNumber].test = false; } } courseNumber++; } else if(InputMatching.matchingInput(line)==2) { int courseIndex = 0; int classIndex = 0; boolean exist = false; boolean createStudent = true; boolean createClass = true; String classID = words[0].substring(0,6); /*獲取課程下標*/ for(int i=0;i<courseNumber;i++){ if(words[2].equals(course[i].getName())){ courseIndex = i; exist = true; break; } } if(!exist){ System.out.println(words[2]+" does not exist"); } for(int i=0;i<classCount;i++){ if(classID.equals(cls[i].getID())){ createClass = false; classIndex = i; break; } } if(createClass){ classIndex = courseNumber-1; cls[classCount] = new Cls(); cls[classCount].setID(classID); classCount++; } for(int i=0;i<studentNumber;i++){ if(words[0].equals(student[i].getID())){ createStudent = false; break; } } if(createStudent) { student[studentNumber] = new Student(); student[studentNumber].setID(words[0]); student[studentNumber].setName(words[1]); if(words.length==4){ if(course[courseIndex].test){ if(exist) System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); student[studentNumber].format = false; }else{ student[studentNumber].setScore(Integer.parseInt(words[3])); course[courseIndex].setTestScore(Integer.parseInt(words[3])); course[courseIndex].setScore(Integer.parseInt(words[3])); cls[classIndex].setScore(Integer.parseInt(words[3])); } }else if(words.length==5){ if(!course[courseIndex].test){ if(exist) System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); student[studentNumber].format = false; }else{ student[studentNumber].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7); cls[classIndex].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7); course[courseIndex].setTestScore(Integer.parseInt(words[4])); course[courseIndex].setTimeScore(Integer.parseInt(words[3])); course[courseIndex].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7); } } studentNumber++; } } else System.out.println("wrong format"); } for(int i=0;i<studentNumber;i++){ if(student[i].getScore()==0){ System.out.println(student[i].getID()+" "+student[i].getName()+ " did not take any exams"); } else{ System.out.println(student[i].getID()+" "+student[i].getName()+" "+ student[i].lastScore()); } } for(int i=0;i<courseNumber;i++){ if(course[i].getScore()==0&&course[i].format){ System.out.println(course[i].getName()+" has no grades yet"); }else{ if(course[i].test&&course[i].format){ System.out.println(course[i].getName()+" "+(int)course[i].getTimeScore()/course[i].getScoreCount()+" "+ (int)course[i].getTestScore()/course[i].getScoreCount()+" "+(int)course[i].getScore()/course[i].getScoreCount()); }else if(course[i].format){ System.out.println(course[i].getName()+" "+ (int)course[i].getTestScore()/course[i].getScoreCount()+" "+(int)course[i].getScore()/course[i].getScoreCount()); } } } for(int i=0;i<classCount;i++){ if(cls[i].getScore()==0){ System.out.println(cls[i].getID()+" has no grades yet"); }else{ System.out.println(cls[i].getID()+" "+(int)cls[i].getScore()/cls[i].getScoreCount()); } } boolean output = false; if(output){ Scanner scanner = new Scanner(System.in); // 存儲所有課程的信息 Map<String, Course> courses = new TreeMap<>(); // 存儲每個學生的成績信息 Map<String, List<Grade>> students = new TreeMap<>(); while (scanner.hasNextLine()) { String line = scanner.nextLine(); String[] parts = line.split(" "); if (parts.length == 3) { // 處理課程信息 String name = parts[0]; String nature = parts[1]; String mode = parts[2]; // 檢查是否有重復的課程信息 // 檢查課程性質和考核方式是否匹配 } else { // 處理成績信息 if(parts[0].equals("end")) break; String id = parts[0]; String name = parts[1]; String courseName = parts[2]; List<Grade> grades = students.get(id); if (grades == null) { grades = new ArrayList<>(); students.put(id, grades); } // 檢查成績信息中的課程是否存在 if (course == null) { System.out.println(id + " " + name + " : " + courseName + " does not exist"); continue; } if (grades.stream().anyMatch(g -> g.courseName.equals(courseName))) { // 已有該門課程的成績信息,忽略后續的成績信息 continue; } int usualScore = -1; int examScore; try { examScore = Integer.parseInt(parts[parts.length - 1]); if (parts.length == 5) { usualScore = Integer.parseInt(parts[3]); } } catch (NumberFormatException e) { System.out.println("wrong format"); continue; } // 檢查成績數量和課程的考核方式是否匹配 Grade grade = new Grade(id, name, courseName, usualScore, examScore); grades.add(grade); } } } } } class Course{ private String name; private double timeScore=0; private double testScore=0; private double score=0; private int scoreCount=0; public boolean test; public boolean required; public boolean format = true; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getTimeScore() { return timeScore; } public void setTimeScore(double timeScore) { this.timeScore += timeScore; } public double getTestScore() { return testScore; } public void setTestScore(double testScore) { this.testScore += testScore; } public double getScore() { return score; } public void setScore(double score) { this.score += score; this.scoreCount++; } public int getScoreCount() { return scoreCount; } } class Student{ private String ID; private String name; private double score; private int scoreCount=0; public boolean format = true; public String getID() { return ID; } public void setID(String ID) { this.ID = ID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getScore() { return score; } public void setScore(double score) { this.score += score; this.scoreCount++; } public int lastScore(){ return (int)score/scoreCount; } } class Cls{ private String ID; private double score; private int scoreCount=0; public void setID(String ID){ this.ID = ID; } public String getID(){ return ID; } public void setScore(double score) { this.score += score; this.scoreCount++; } public double getScore(){ return score; } public int getScoreCount() { return scoreCount; } } class InputMatching { static String stuNumMatching = "[0-9]{8}";//8個0-9的數字 static String stuNameMatching = "\\S{1,10}";//1到10個非空格(TAB)字符 static String scoreMatching = "([1-9]?[0-9]|100)"; static String courseNameMatching = "\\S{1,10}";//1到10個非空格(TAB)字符 static String courseTypeMatching = "(選修|必修)"; static String checkCourseTypeMatching = "(考試|考察)"; static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkCourseTypeMatching; /*courseInput用于定義課程信息模式(正則表達式)*/ static String scoreInput = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching + "(scoreMatching)?"; /*scoreInput用于定義成績信息模式(正則表達式)*/ public static int matchingInput(String s) { if (matchingCourse(s)) { return 1; } if (matchingScore(s)||s.startsWith("2")) { return 2; } return 0; } private static boolean matchingCourse(String s) { return s.matches(courseInput); } private static boolean matchingScore(String s) { return s.matches(scoreInput); } } class Course14 { String name; String nature; String mode; public void Course(String name, String nature, String mode) { this.name = name; this.nature = nature; this.mode = mode; } } class Grade { String id; String name; String courseName; int usualScore; int examScore; public Grade(String id, String name, String courseName, int usualScore, int examScore) { this.id = id; this.name = name; this.courseName = courseName; this.usualScore = usualScore; this.examScore = examScore; } public int getScore() { if (usualScore == -1 || examScore == -1) { return -1; // 成績不合法 } return (int)Math.round(usualScore * 0.3 + examScore * 0.7); // 四舍五入取整 } }
課程成績統計程序-2
1)類圖

2)分析
該代碼是一個學生成績管理系統,與上一個代碼相比,做了一些修改和優化。主要改動如下:
1. 在課程信息錄入部分,增加了"實驗"作為課程類型,并對該類型課程進行了單獨處理。
2. 在學生成績錄入部分,對輸入的成績進行了一些合法性校驗和錯誤處理。如果成績超過100或小于0,則輸出錯誤提示信息。
3. 根據輸入行中的長度判斷是否存在多個成績,如果存在,則對多個成績進行處理,計算平均值。
4. 在輸出部分,對學生列表、課程列表和班級列表進行了排序。學生列表按照學號升序排列,課程列表按照課程名稱升序排列,班級列表按照班級號升序排列。
5. 修改了InputMatching類中正則表達式的定義,以適應新的輸入格式。
6. 刪除了一些沒有使用的變量和代碼塊。
總體來說,該版本的代碼在功能和邏輯上與上一個版本相似,但進行了一些優化和調整,使得代碼更加清晰和簡潔。同時,對輸入的成績進行了合法性校驗和錯誤處理,對輸出結果進行了排序,提高了程序的穩定性和可讀性。
3)源碼
import java.util.*; import java.text.Collator; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Course[] course = new Course[100]; Student[] student = new Student[100]; Cls[] cls = new Cls[100]; int fsfu = 0; //課程數量 int afhas = 0; //學生數量 int classCount = 0; //班級數量 /*獲取輸入*/ StringBuilder sb = new StringBuilder(); HashSet<String> set = new HashSet<>(); while (sc.hasNextLine()) { String s = sc.nextLine(); if (s.equals("end")) { break; } if (set.add(s)) { // 添加成功則說明是第一次出現的行,才將其加入 StringBuilder 中 sb.append(s).append("\n"); } } String str = sb.toString(); String[] lines = str.split("\n"); for(String line : lines) { String[] words = line.split(" "); boolean repeat = false; if(InputMatching.matchingInput(line)==1) { for(int i=0;i<fsfu;i++){ if(words[0].equals(course[i].getName())){ repeat = true; break; } }if(repeat) continue; course[fsfu] = new Course(); course[fsfu].setName(words[0]); /*選修與必修*/ switch (words[1]) { case "必修": course[fsfu].required = true; break; case "選修": course[fsfu].required = false; break; case "實驗": course[classCount].pre = true; break; } /*考試與考察與實驗*/ boolean pra = false; if(words[1].equals("實驗")){ pra = true; if(words[2].equals("實驗")){ course[fsfu].pre = true; }else { course[fsfu].format = false; System.out.println(words[0]+" : course type & access mode mismatch"); course[fsfu].setName(null); } } if(course[fsfu].required&&!pra) { course[fsfu].test = true; if(words[2].equals("考察")||words[2].equals("實驗")){ course[fsfu].format = false; System.out.println(words[0]+" : course type & access mode mismatch"); course[fsfu].setName(null); } }else if(!pra){ switch (words[2]) { case "考試": course[fsfu].test = true; break; case "考察": course[fsfu].test = false; break; case "實驗": course[fsfu].format = false; System.out.println(words[0] + " : course type & access mode mismatch"); course[fsfu].setName(null); break; } } fsfu++; } else if(InputMatching.matchingInput(line)==2||words.length>=5) { boolean mistake = false; if(Integer.parseInt(words[3])>100){ System.out.println("wrong format"); continue; } if(words.length>5){ if(Integer.parseInt(words[3])<4||Integer.parseInt(words[3])>9){ System.out.println("wrong format"); continue; }try { for (int i = 0; i < Integer.parseInt(words[3]); i++) { if (Integer.parseInt(words[i + 4]) > 100 || Integer.parseInt(words[i + 4]) < 0) { mistake = true; } } }catch (ArrayIndexOutOfBoundsException ignored){ } if(mistake) { System.out.println("wrong format"); continue; } } int dgjhds = 0; int gdghjds = 0; int studentIndex = 0; boolean exist = false; boolean createStudent = true; boolean createClass = true; String classID = words[0].substring(0,6); /*獲取課程下標*/ for(int i=0;i<fsfu;i++){ if(words[2].equals(course[i].getName())){ dgjhds = i; exist = true; break; } } /*不存在課程*/ if(!exist){ System.out.println(words[2]+" does not exist"); } /*已存在該班*/ for(int i=0;i<classCount;i++){ if(classID.equals(cls[i].getID())){ createClass = false; gdghjds = i; break; } } if(createClass){ gdghjds = classCount; cls[classCount] = new Cls(); cls[classCount].setID(classID); classCount++; } /*已存在該學生*/ for(int i=0;i<afhas;i++){ if(words[0].equals(student[i].getID())){ createStudent = false; studentIndex = i; break; } } if(createStudent) { student[afhas] = new Student(); student[afhas].setID(words[0]); student[afhas].setName(words[1]); if(words.length==4){ if(course[dgjhds].pre){ System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); continue; } if(course[dgjhds].test){ if(exist) System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); student[afhas].format = false; }else{ student[afhas].setScore(Integer.parseInt(words[3])); course[dgjhds].setTestScore(Integer.parseInt(words[3])); course[dgjhds].setScore(Integer.parseInt(words[3])); cls[gdghjds].setScore(Integer.parseInt(words[3])); } }else if(words.length==5){ if(course[dgjhds].pre){ System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); continue; } if(!course[dgjhds].test){ if(exist) System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); student[afhas].format = false; }else{ student[afhas].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7); cls[gdghjds].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7); course[dgjhds].setTestScore(Integer.parseInt(words[4])); course[dgjhds].setTimeScore(Integer.parseInt(words[3])); course[dgjhds].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7); } }else if(words.length<8){ System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); }else { double theWhole = 0; double theAverage; int gdsfdshgd = Integer.parseInt(words[3]); if(words.length-4 != gdsfdshgd){ System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); continue; } for(int i = 0;i<gdsfdshgd;i++){ theWhole += Integer.parseInt(words[i+4]); } theAverage = theWhole / gdsfdshgd; student[afhas].setScore(theAverage); course[dgjhds].setScore(theAverage); cls[gdghjds].setScore(theAverage); } afhas++; }else { if(words.length==4){ if(course[dgjhds].test){ if(exist) System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); student[studentIndex].format = false; }else{ student[studentIndex].setScore(Integer.parseInt(words[3])); course[dgjhds].setTestScore(Integer.parseInt(words[3])); course[dgjhds].setScore(Integer.parseInt(words[3])); cls[gdghjds].setScore(Integer.parseInt(words[3])); } }else if(words.length==5){ if(!course[dgjhds].test){ if(exist) System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); student[studentIndex].format = false; }else{ student[studentIndex].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7); cls[gdghjds].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7); course[dgjhds].setTestScore(Integer.parseInt(words[4])); course[dgjhds].setTimeScore(Integer.parseInt(words[3])); course[dgjhds].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7); } }else if(words.length<8){ System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); } else { double theWhole = 0; double theAverage; int gdsfdshgd = Integer.parseInt(words[3]); if(words.length-4 != gdsfdshgd){ System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); continue; } for(int i = 0;i<gdsfdshgd;i++){ theWhole += Integer.parseInt(words[i+4]); } theAverage = theWhole / gdsfdshgd; student[afhas].setScore(theAverage); course[dgjhds].setScore(theAverage); cls[gdghjds].setScore(theAverage); } } }else System.out.println("wrong format"); } List<Student> studentList = new ArrayList<>(Arrays.asList(student).subList(0, afhas)); List<Cls> clsList = new ArrayList<>(Arrays.asList(cls).subList(0, classCount)); List<Course> courseList = new ArrayList<>(Arrays.asList(course).subList(0, fsfu)); Collections.sort(studentList); Collections.sort(clsList); courseList.sort(new CourseComparator()); for(Student students : studentList){ if(students.getScore()==0){ System.out.println(students.getID()+" "+students.getName()+ " did not take any exams"); } else{ System.out.println(students.getID()+" "+students.getName()+" "+ students.lastScore()); } } for(Course course1 : courseList){ if(course1.getScore()==0&&course1.format){ System.out.println(course1.getName()+" has no grades yet"); }else{ if(course1.pre&&course1.format){ System.out.println(course1.getName()+" "+ (int)course1.getScore()/course1.getScoreCount()); } else if(course1.test&&course1.format){ System.out.println(course1.getName()+" "+(int)course1.getTimeScore()/course1.getScoreCount()+" "+ (int)course1.getTestScore()/course1.getScoreCount()+" "+(int)course1.getScore()/course1.getScoreCount()); }else if(course1.format){ System.out.println(course1.getName()+" "+ (int)course1.getTestScore()/course1.getScoreCount()+" "+(int)course1.getScore()/course1.getScoreCount()); } } } for(Cls cls1 : clsList){ if(cls1.getScore()==0){ System.out.println(cls1.getID()+" has no grades yet"); }else{ System.out.println(cls1.getID()+" "+(int)cls1.getScore()/cls1.getScoreCount()); } } boolean useful = false; if(useful){ HashMap<String,String> map = new HashMap<>(); String str2; while(true) { str = sc.nextLine(); if(str.equals("end")){ break; } String []arr = str.split(" "); String num = arr[0]; String name = arr[1]; String grade = arr[2]; if(map.containsKey(num)){ System.out.println("學號重復,請重新輸入"); } else { map.put(num,name+" "+grade); } } String s = sc.nextLine(); if(map.containsKey(s)){ String []arr = map.get(s).split(" "); System.out.println(s+" "+arr[0]+" "+arr[1]); } else { System.out.println("The student "+s+" does not exist"); } } Map<Integer, String[]> map = new HashMap<>(); //使用HashMap存儲學生信息,鍵為學號,值為字符串數組,依次存儲學號、姓名、成績 while (true) { String[] input = sc.nextLine().split(" "); if (input[0].equals("end")) { break; } int id = Integer.parseInt(input[0]); String[] info = {input[1], input[2]}; //將姓名和成績存儲在一個字符串數組中 map.put(id, info); } List<Integer> ids = new ArrayList<>(map.keySet()); Collections.sort(ids, Collections.reverseOrder()); //按學號從大到小排序 for (int id : ids) { String[] info = map.get(id); System.out.println(id + " " + info[0] + " " + info[1]); //依次輸出學號、姓名、成績 } } } class CourseComparator implements Comparator<Course> { @Override public int compare(Course c1, Course c2) { Collator collator = Collator.getInstance(Locale.CHINA); return collator.compare(c1.getName(), c2.getName()); } } class Course implements Comparable<Course>{ private String name; private double timeScore=0; private double testScore=0; private double score=0; private int scoreCount=0; public boolean test; public boolean required; public boolean format = true; public boolean pre; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getTimeScore() { return timeScore; } public void setTimeScore(double timeScore) { this.timeScore += timeScore; } public double getTestScore() { return testScore; } public void setTestScore(double testScore) { this.testScore += testScore; } public double getScore() { return score; } public void setScore(double score) { this.score += score; this.scoreCount++; } public int getScoreCount() { return scoreCount; } // 實現Comparable接口中的compareTo方法,按照課程名稱字符順序排列 @Override public int compareTo(Course other) { return this.name.compareTo(other.getName()); } } class Student implements Comparable<Student>{ private String ID; private String name; private double score; private int scoreCount=0; public boolean format = true; public String getID() { return ID; } public void setID(String ID) { this.ID = ID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getScore() { return score; } public void setScore(double score) { this.score += score; this.scoreCount++; } public int lastScore(){ return (int)score/scoreCount; } // 定義比較規則:按照id升序排列 @Override public int compareTo(Student o) { return Integer.compare(Integer.parseInt(this.ID), Integer.parseInt(o.ID)); } } class Cls implements Comparable<Cls>{ private String ID; private double score; private int scoreCount=0; public void setID(String ID){ this.ID = ID; } public String getID(){ return ID; } public void setScore(double score) { this.score += score; this.scoreCount++; } public double getScore(){ return score; } public int getScoreCount() { return scoreCount; } // 定義比較規則:按照id升序排列 @Override public int compareTo(Cls o) { return Integer.compare(Integer.parseInt(this.ID), Integer.parseInt(o.ID)); } } class InputMatching { static String stuNumMatching = "[0-9]{8}";//8個0-9的數字 static String stuNameMatching = "\\S{1,10}";//1到10個非空格(TAB)字符 static String scoreMatching = "([1-9]?[0-9]|100)"; static String courseNameMatching = "\\S{1,10}";//1到10個非空格(TAB)字符 static String courseTypeMatching = "(選修|必修|實驗)"; static String checkCourseTypeMatching = "(考試|考察|實驗)"; static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkCourseTypeMatching; /*courseInput用于定義課程信息模式(正則表達式)*/ static String scoreInput = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching + "(scoreMatching)?"; static String scoreInput2 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching + "(scoreMatching)?" + " " + scoreMatching + "(scoreMatching)?"; public static int matchingInput(String s) { if (matchingCourse(s)) { return 1; } if (matchingScore(s)) { return 2; } return 0; } private static boolean matchingCourse(String s) { return s.matches(courseInput); } private static boolean matchingScore(String s) { return s.matches(scoreInput)||s.matches(scoreInput2); } }
課程成績統計程序-3
1)類圖

2)分析
該代碼實現了一個學生選課成績管理系統。主要功能包括輸入課程信息和學生成績,然后按照一定規則進行處理和排序,并最終輸出結果。
在Main類中,首先通過Scanner從控制臺獲取輸入的課程信息和學生成績,然后根據不同的輸入情況進行處理和計算。最后將處理后的結果按照一定的排序規則輸出。
Course類封裝了課程的相關信息和成績,包括課程名稱、考試成績、平時成績等。實現了Comparable接口,通過課程名稱進行排序。
Student類封裝了學生的相關信息和成績,包括學生ID、姓名、成績等。實現了Comparable接口,通過學生ID進行排序。
Cls類封裝了班級的相關信息和成績,包括班級ID和班級平均成績。實現了Comparable接口,通過班級ID進行排序。
InputMatching類用于檢查輸入的格式是否符合要求,定義了一系列正則表達式來進行匹配。
CourseComparator類實現了Comparator接口,用于對課程進行排序,按照課程名稱字符順序排列。
綜上所述,該代碼實現了學生選課成績管理系統,并通過合適的數據結構和排序算法對數據進行處理和輸出。
3)源碼
import java.util.*; import java.text.Collator; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Course[] course = new Course[100]; Student[] student = new Student[100]; Cls[] cls = new Cls[100]; int courseCount = 0; //課程數量 int studentCount = 0; //學生數量 int classCount = 0; //班級數量 /*獲取輸入*/ StringBuilder sb = new StringBuilder(); HashSet<String> set = new HashSet<>(); while (sc.hasNextLine()) { String s = sc.nextLine(); if (s.equals("end")) { break; } if (set.add(s)) { // 添加成功則說明是第一次出現的行,才將其加入 StringBuilder 中 sb.append(s).append("\n"); } } String str = sb.toString(); String[] lines = str.split("\n"); for(String line : lines) { String[] words = line.split(" "); boolean repeat = false; if(InputMatching.matchingInput(line)==1) { for(int i=0;i<courseCount;i++){ if(words[0].equals(course[i].getName())){ repeat = true; break; } }if(repeat) continue; course[courseCount] = new Course(); course[courseCount].setName(words[0]); /*選修與必修*/ switch (words[1]) { case "必修": course[courseCount].required = true; break; case "選修": course[courseCount].required = false; break; case "實驗": course[classCount].pre = true; break; } /*考試與考察與實驗*/ boolean pra = false; if(words[1].equals("實驗")){ pra = true; if(words[2].equals("實驗")){ course[courseCount].pre = true; }else { course[courseCount].format = false; System.out.println(words[0]+" : course type & access mode mismatch"); course[courseCount].setName(null); } } if(course[courseCount].required&&!pra) { course[courseCount].test = true; if(words[2].equals("考察")||words[2].equals("實驗")){ course[courseCount].format = false; System.out.println(words[0]+" : course type & access mode mismatch"); course[courseCount].setName(null); } }else if(!pra){ switch (words[2]) { case "考試": course[courseCount].test = true; break; case "考察": course[courseCount].test = false; break; case "實驗": course[courseCount].format = false; System.out.println(words[0] + " : course type & access mode mismatch"); course[courseCount].setName(null); break; } } courseCount++; } else if(InputMatching.matchingInput(line)==2||words.length>=5) { boolean mistake = false; if(Integer.parseInt(words[3])>100){ System.out.println("wrong format"); continue; } if(words.length>5){ if(Integer.parseInt(words[3])<4||Integer.parseInt(words[3])>9){ System.out.println("wrong format"); continue; }try { for (int i = 0; i < Integer.parseInt(words[3]); i++) { if (Integer.parseInt(words[i + 4]) > 100 || Integer.parseInt(words[i + 4]) < 0) { mistake = true; } } }catch (ArrayIndexOutOfBoundsException ignored){ } if(mistake) { System.out.println("wrong format"); continue; } } int courseIndex = 0; int classIndex = 0; int studentIndex = 0; boolean exist = false; boolean createStudent = true; boolean createClass = true; String classID = words[0].substring(0,6); /*獲取課程下標*/ for(int i=0;i<courseCount;i++){ if(words[2].equals(course[i].getName())){ courseIndex = i; exist = true; break; } } /*不存在課程*/ if(!exist){ System.out.println(words[2]+" does not exist"); } /*已存在該班*/ for(int i=0;i<classCount;i++){ if(classID.equals(cls[i].getID())){ createClass = false; classIndex = i; break; } } if(createClass){ classIndex = classCount; cls[classCount] = new Cls(); cls[classCount].setID(classID); classCount++; } /*已存在該學生*/ for(int i=0;i<studentCount;i++){ if(words[0].equals(student[i].getID())){ createStudent = false; studentIndex = i; break; } } if(createStudent) { student[studentCount] = new Student(); student[studentCount].setID(words[0]); student[studentCount].setName(words[1]); if(words.length==4){ if(course[courseIndex].pre){ System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); continue; } if(course[courseIndex].test){ if(exist) System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); student[studentCount].format = false; }else{ student[studentCount].setScore(Integer.parseInt(words[3])); course[courseIndex].setTestScore(Integer.parseInt(words[3])); course[courseIndex].setScore(Integer.parseInt(words[3])); cls[classIndex].setScore(Integer.parseInt(words[3])); } }else if(words.length==5){ if(course[courseIndex].pre){ System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); continue; } if(!course[courseIndex].test){ if(exist) System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); student[studentCount].format = false; }else{ student[studentCount].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7); cls[classIndex].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7); course[courseIndex].setTestScore(Integer.parseInt(words[4])); course[courseIndex].setTimeScore(Integer.parseInt(words[3])); course[courseIndex].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7); } }else if(words.length<8){ System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); }else { double theWhole = 0; double theAverage; int thePre = Integer.parseInt(words[3]); if(words.length-4 != thePre){ System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); continue; } for(int i = 0;i<thePre;i++){ theWhole += Integer.parseInt(words[i+4]); } theAverage = theWhole / thePre; student[studentCount].setScore(theAverage); course[courseIndex].setScore(theAverage); cls[classIndex].setScore(theAverage); } studentCount++; }else { if(words.length==4){ if(course[courseIndex].test){ if(exist) System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); student[studentIndex].format = false; }else{ student[studentIndex].setScore(Integer.parseInt(words[3])); course[courseIndex].setTestScore(Integer.parseInt(words[3])); course[courseIndex].setScore(Integer.parseInt(words[3])); cls[classIndex].setScore(Integer.parseInt(words[3])); } }else if(words.length==5){ if(!course[courseIndex].test){ if(exist) System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); student[studentIndex].format = false; }else{ student[studentIndex].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7); cls[classIndex].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7); course[courseIndex].setTestScore(Integer.parseInt(words[4])); course[courseIndex].setTimeScore(Integer.parseInt(words[3])); course[courseIndex].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7); } }else if(words.length<8){ System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); } else { double theWhole = 0; double theAverage; int thePre = Integer.parseInt(words[3]); if(words.length-4 != thePre){ System.out.println(words[0]+" "+words[1]+" : access mode mismatch"); continue; } for(int i = 0;i<thePre;i++){ theWhole += Integer.parseInt(words[i+4]); } theAverage = theWhole / thePre; student[studentCount].setScore(theAverage); course[courseIndex].setScore(theAverage); cls[classIndex].setScore(theAverage); } } }else System.out.println("wrong format"); } List<Student> studentList = new ArrayList<>(Arrays.asList(student).subList(0, studentCount)); List<Cls> clsList = new ArrayList<>(Arrays.asList(cls).subList(0, classCount)); List<Course> courseList = new ArrayList<>(Arrays.asList(course).subList(0, courseCount)); Collections.sort(studentList); Collections.sort(clsList); courseList.sort(new CourseComparator()); for(Student students : studentList){ if(students.getScore()==0){ System.out.println(students.getID()+" "+students.getName()+ " did not take any exams"); } else{ System.out.println(students.getID()+" "+students.getName()+" "+ students.lastScore()); } } for(Course course1 : courseList){ if(course1.getScore()==0&&course1.format){ System.out.println(course1.getName()+" has no grades yet"); }else{ if(course1.pre&&course1.format){ System.out.println(course1.getName()+" "+ (int)course1.getScore()/course1.getScoreCount()); } else if(course1.test&&course1.format){ System.out.println(course1.getName()+" "+(int)course1.getTimeScore()/course1.getScoreCount()+" "+ (int)course1.getTestScore()/course1.getScoreCount()+" "+(int)course1.getScore()/course1.getScoreCount()); }else if(course1.format){ System.out.println(course1.getName()+" "+ (int)course1.getTestScore()/course1.getScoreCount()+" "+(int)course1.getScore()/course1.getScoreCount()); } } } for(Cls cls1 : clsList){ if(cls1.getScore()==0){ System.out.println(cls1.getID()+" has no grades yet"); }else{ System.out.println(cls1.getID()+" "+(int)cls1.getScore()/cls1.getScoreCount()); } } } } class CourseComparator implements Comparator<Course> { @Override public int compare(Course c1, Course c2) { Collator collator = Collator.getInstance(Locale.CHINA); return collator.compare(c1.getName(), c2.getName()); } } class Course implements Comparable<Course>{ private String name; private double timeScore=0; private double testScore=0; private double score=0; private int scoreCount=0; public boolean test; public boolean required; public boolean format = true; public boolean pre; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getTimeScore() { return timeScore; } public void setTimeScore(double timeScore) { this.timeScore += timeScore; } public double getTestScore() { return testScore; } public void setTestScore(double testScore) { this.testScore += testScore; } public double getScore() { return score; } public void setScore(double score) { this.score += score; this.scoreCount++; } public int getScoreCount() { return scoreCount; } // 實現Comparable接口中的compareTo方法,按照課程名稱字符順序排列 @Override public int compareTo(Course other) { return this.name.compareTo(other.getName()); } } class Student implements Comparable<Student>{ private String ID; private String name; private double score; private int scoreCount=0; public boolean format = true; public String getID() { return ID; } public void setID(String ID) { this.ID = ID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getScore() { return score; } public void setScore(double score) { this.score += score; this.scoreCount++; } public int lastScore(){ return (int)score/scoreCount; } // 定義比較規則:按照id升序排列 @Override public int compareTo(Student o) { return Integer.compare(Integer.parseInt(this.ID), Integer.parseInt(o.ID)); } } class Cls implements Comparable<Cls>{ private String ID; private double score; private int scoreCount=0; public void setID(String ID){ this.ID = ID; } public String getID(){ return ID; } public void setScore(double score) { this.score += score; this.scoreCount++; } public double getScore(){ return score; } public int getScoreCount() { return scoreCount; } // 定義比較規則:按照id升序排列 @Override public int compareTo(Cls o) { return Integer.compare(Integer.parseInt(this.ID), Integer.parseInt(o.ID)); } } class InputMatching { static String stuNumMatching = "[0-9]{8}";//8個0-9的數字 static String stuNameMatching = "\\S{1,10}";//1到10個非空格(TAB)字符 static String scoreMatching = "([1-9]?[0-9]|100)"; static String courseNameMatching = "\\S{1,10}";//1到10個非空格(TAB)字符 static String courseTypeMatching = "(選修|必修|實驗)"; static String checkCourseTypeMatching = "(考試|考察|實驗)"; static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkCourseTypeMatching; /*courseInput用于定義課程信息模式(正則表達式)*/ static String scoreInput = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching + "(scoreMatching)?"; static String scoreInput2 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching + "(scoreMatching)?" + " " + scoreMatching + "(scoreMatching)?"; public static int matchingInput(String s) { if (matchingCourse(s)) { return 1; } if (matchingScore(s)) { return 2; } return 0; } private static boolean matchingCourse(String s) { return s.matches(courseInput); } private static boolean matchingScore(String s) { return s.matches(scoreInput)||s.matches(scoreInput2); } }
三、主要困難及改進建議。
主要困難:
1. 輸入合法性驗證:代碼中對輸入的合法性進行了一些驗證和錯誤處理。然而,這部分代碼仍然比較冗長,可讀性較差,很多重復的判斷邏輯。這可能會導致代碼的維護和擴展困難。
2. 代碼重復:代碼中存在一些重復的判斷邏輯和重復的代碼塊,使得代碼的冗余度較高,可讀性較差,也增加了代碼維護的難度。
改進的建議:
1. 提取公共方法:將輸入合法性驗證和錯誤處理的邏輯提取為獨立的方法,以提高代碼的可讀性和可維護性。可以考慮設計一個輸入處理類,將輸入的合法性判斷、錯誤處理和信息存儲等功能封裝在該類中,使得代碼更加模塊化和易于擴展。
2. 減少重復代碼:通過提取公共方法、使用循環和條件語句等方式,減少代碼中的重復邏輯和重復代碼的存在。可以通過抽象出公共的判斷和處理邏輯,將其封裝成獨立的方法或工具類,方便復用和維護。
3. 引入異常處理:對于輸入合法性驗證和錯誤處理,可以引入異常處理機制,使用自定義異常類來表示不合法的輸入和錯誤情況,從而增加代碼的可讀性和可維護性。
4. 簡化數據結構:通過優化數據結構的設計,減少不必要的屬性和方法,可以提高代碼的簡潔性和可讀性。比如,可以考慮使用Map來存儲學生和課程信息,以簡化代碼的邏輯。
綜上所述,改進的主要方向是簡化代碼邏輯、減少重復代碼和提高代碼的可讀性和可維護性。通過引入更好的設計模式和編碼規范,可以使代碼更加簡潔和易于理解。
四、總結
這兩段代碼都是一個學生成績管理系統,能夠錄入課程信息和學生成績,并進行統計分析和輸出結果。
第一段代碼主要采用面向對象的思想,通過類和對象對課程、學生和班級的信息進行封裝和操作。代碼中使用了正則表達式對輸入的合法性進行驗證,并進行相應的錯誤處理。然而,代碼存在一些冗余和不必要的重復邏輯,可讀性和可維護性有所欠缺。針對這些問題,建議提取公共方法、減少重復代碼、引入異常處理機制,并簡化數據結構的設計。
第二段代碼在第一段代碼的基礎上進行了一些優化和調整。主要改進包括增加了"實驗"作為課程類型的處理、對成績進行合法性校驗和錯誤處理、對輸出結果進行排序等。然而,代碼中仍然存在一些冗余和重復的邏輯,可讀性和可維護性仍有改進的空間。建議進一步提取公共方法、減少重復代碼,并通過異常處理、數據結構設計和代碼簡化等方面進行優化。
總的來說,這兩段代碼都是一個學生成績管理系統的簡單實現,存在一些改進的空間。通過優化代碼邏輯、減少重復代碼和提高代碼可讀性和可維護性,可以使代碼更加簡潔、易于理解和擴展。同時,根據實際需求,可以進一步優化輸入處理、異常處理以及數據結構的設計等方面,以提升系統的穩定性和擴展性。
學到了:
1. 了解了一個學生成績管理系統的基本功能和實現方式。
2. 熟悉了使用面向對象的思想進行代碼設計和封裝,以及使用類和對象管理數據。
3. 掌握了使用正則表達式對輸入合法性進行驗證的方法。
4. 學會了使用循環、條件語句和數組等基本編程技巧處理輸入數據和進行相應的操作。
5. 理解了優化代碼的重要性,如提取公共方法、減少重復代碼、簡化邏輯和數據結構等,以提高代碼的可讀性、可維護性和性能。
6. 意識到代碼中存在的一些問題,如冗余邏輯、重復代碼和不合理的數據結構設計。
需要進一步學習和研究的方面包括:
1. 異常處理:深入學習異常處理的原理和機制,在代碼中靈活運用異常處理來提高代碼的健壯性和可維護性。
2. 設計模式:學習和掌握常用的設計模式,以提高代碼的可重用性、可擴展性和可維護性。
3. 大型項目架構和設計:了解大型項目的架構設計原則、模塊劃分、層次結構等,學習如何進行系統性的項目規劃和架構設計。
4. 性能優化:進一步學習如何進行代碼和程序的性能優化,減少資源的消耗,并提高系統的響應速度和穩定性。
5. 設計原則和編碼規范:學習常用的設計原則和編碼規范,如單一職責原則、開閉原則、代碼重構等,用于指導和規范自己的代碼編寫。
浙公網安備 33010602011771號