<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                > 考試說明:本次測試卷一共1道測試題,共計50分。考試時間120分鐘。 ## 一、編程題(共1題,共計50分) 編寫代碼完成如下要求: * 完成學生類 Student 的編寫 ``` 屬性:姓名、年齡、學科、成績等 ``` ``` package test; import java.io.Serializable; import java.util.Objects; public class Student implements Serializable, Comparable<Student> { private String code; private String name; private String subject; private int score; public Student() {} public Student(String code, String subject) { this.code = code; this.subject = subject; } public Student(String code, String name, String subject, int score) { this.code = code; this.name = name; this.subject = subject; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } @Override public String toString() { return "學號:" + code + " " + "姓名:" + name + " " + "學科:" + subject + " " + "得分:" + score; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return Objects.equals(code, student.code) && Objects.equals(subject, student.subject); } @Override public int hashCode() { return Objects.hash(code, subject); } @Override public int compareTo(Student s) { if (this.getSubject().equals(s.getSubject())) { return s.getScore() - this.getScore(); } else { return s.getSubject().compareTo(this.getSubject()); } } } ``` * 完成文件操作類 FileOperate 的編寫 ``` 功能:void write(List<T> list) 將集合數據寫入文件 List<T> read(File file) 讀取文件中存儲的集合數據 ``` ``` package test; import java.io.*; import java.util.ArrayList; import java.util.List; public class FileOperate { public static <T> void write(List<T> list, File target) { if (!target.exists()) { try { target.createNewFile(); } catch (IOException e) { e.printStackTrace(); return; } } FileOutputStream fos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream(target); oos = new ObjectOutputStream(fos); oos.writeObject(list); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (oos != null) { try { oos.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static <T> List<T> read(File file) { List<T> list = new ArrayList<>(); if (!file.exists()) { return list; } FileInputStream fis = null; ObjectInputStream ois = null; try { fis = new FileInputStream(file); ois = new ObjectInputStream(fis); list = (List<T>) ois.readObject(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (ois != null) { try { ois.close(); } catch (IOException e) { e.printStackTrace(); } } if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } return list; } } ``` * 完成學生管理類 StudentManage 的編寫 ``` 功能: 1. 控制臺錄入學生考試成績(學號、姓名、學科、成績),退出當前菜單時將學生信息保存至文件中 2. 控制臺修改學生考試成績(學號、學科),退出當前菜單時將學生信息保存至文件中 3. 控制臺查看指定學科的全部學生成績(從高到低逆序排序) 4. 控制臺查看指定學生的全部成績信息(從高到低逆序排序) ``` ``` package test; import java.io.File; import java.util.*; public class StudentManage { public static final File STUDENT_SCORE = new File("d:/stu_score.data"); public static List<Student> students; static { getStudents(); } public static List<Student> getStudents() { students = FileOperate.read(STUDENT_SCORE); return students; } /** * 根據姓名獲取學生 * @param name * @return */ public static List<Student> getStudentsByName(String name) { List<Student> list = new ArrayList<>(); for (Student s : students) { if (s.getName().contains(name)) { list.add(s); } } Collections.sort(list); return list; } /** * 根據學號獲取學生 * @param code * @return */ public static List<Student> getStudentsByCode(String code) { List<Student> list = new ArrayList<>(); for (Student s : students) { if (s.getCode().equals(code)) { list.add(s); } } Collections.sort(list); return list; } /** * 根據學科獲取學生 * @param subject * @return */ public static List<Student> getStudentsBySubject(String subject) { List<Student> list = new ArrayList<>(); for (Student s : students) { if (s.getSubject().equals(subject)) { list.add(s); } } Collections.sort(list); return list; } // 打印學生信息 public static void printStudents(List<Student> list) { System.out.println(); for (Student s : list) { System.out.println(s); } System.out.println(); } // 打印默認信息 public static void printDefault() { System.out.println(); System.out.println("輸入有誤,請重新輸入!"); System.out.println(); } // 打印輸出不匹配信息 public static void printInputMismatch() { System.out.println(); System.out.println("輸入不匹配,請重新輸入!"); System.out.println(); } // 錄入學生成績 public static void registerStudentInfo() { Scanner sc = new Scanner(System.in); while (true) { System.out.println(); System.out.println("1. 開始錄入學生成績"); System.out.println("0. 退出錄入學生成績"); System.out.println(); System.out.print("請輸入:"); try { int input = sc.nextInt(); if (input == 0) { FileOperate.write(students, STUDENT_SCORE); break; } if (input == 1) { System.out.println(); System.out.print("請輸入學生學號:"); String code = sc.next(); System.out.print("請輸入學生姓名:"); String name = sc.next(); System.out.print("請輸入考試科目:"); String subject = sc.next(); System.out.print("請輸入考試成績:"); int score = sc.nextInt(); Student stu = new Student(code, name, subject, score); if (students.contains(stu)) students.remove(stu); students.add(stu); } else { printDefault(); } } catch (InputMismatchException e) { printInputMismatch(); } } } // 修改學生信息 public static void modifyStudentInfo() { Scanner sc = new Scanner(System.in); while (true) { System.out.println(); System.out.println("1. 開始修改學生成績"); System.out.println("0. 退出修改學生成績"); System.out.println(); System.out.print("請輸入:"); try { int input = sc.nextInt(); if (input == 0) { FileOperate.write(students, STUDENT_SCORE); break; } if (input == 1) { System.out.println(); System.out.print("請輸入學生學號:"); String code = sc.next(); System.out.print("請輸入考試科目:"); String subject = sc.next(); Student stu = new Student(code, subject); int index = students.indexOf(stu); if (index > -1) { System.out.print("請輸入考試成績:"); int score = sc.nextInt(); Student s = students.get(index); s.setScore(score); students.set(index, s); } else { System.out.println(); System.out.println("該學生該學科成績還未錄入"); } } else { printDefault(); } } catch (InputMismatchException e) { printInputMismatch(); } } } // 查看學生成績 public static void getStudentInfo() { Scanner sc = new Scanner(System.in); while (true) { System.out.println(); System.out.println("1. 查看所有學生考試信息"); System.out.println("2. 查看指定學生考試信息"); System.out.println("3. 查看指定科目考試信息"); System.out.println("0. 退出查看學生考試信息"); System.out.println(); System.out.print("請輸入:"); try { int input = sc.nextInt(); if (input == 0) break; switch (input) { case 1: { getStudents(); printStudents(students); break; } case 2: { System.out.println(); System.out.print("請輸入學生姓名:"); List<Student> list = getStudentsByName(sc.next()); printStudents(list); break; } case 3: { System.out.println(); System.out.print("請輸入考試科目:"); List<Student> list = getStudentsBySubject(sc.next()); printStudents(list); break; } default: { printDefault(); } } } catch (InputMismatchException e) { printInputMismatch(); } } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { System.out.println(); System.out.println("1. 錄入學生考試信息"); System.out.println("2. 修改學生考試信息"); System.out.println("3. 查看學生考試信息"); System.out.println("0. 退出學生考試信息"); System.out.println(); System.out.print("請輸入:"); try { int input = sc.nextInt(); if (input == 0) break; switch (input) { case 1: { // 錄入學生成績 registerStudentInfo(); break; } case 2: { // 修改學生成績 modifyStudentInfo(); break; } case 3: { // 查看學生成績 getStudentInfo(); break; } default: { printDefault(); } } } catch (InputMismatchException e) { printInputMismatch(); } } } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看