21xrx.com
2024-12-22 23:11:46 Sunday
登录
文章检索 我的文章 写文章
Java期末考试题:实现一个简单的成绩管理系统
2023-06-18 22:41:31 深夜i     --     --
Java期末考试题 成绩管理系统 学生信息操作

Java是目前广泛应用于软件开发领域的编程语言之一,对于学习Java的学生而言,期末考试是必不可少的一项重要考验。在本文中,我们将介绍一道Java期末考试题,这道题目要求实现一个简单的成绩管理系统。

题目要求如下:

1.定义一个类`Student`,包含以下属性:姓名、年龄、成绩。

2.定义一个类`StudentSystem`,包含如下功能:

(1)添加学生信息

(2)修改学生信息

(3)删除学生信息

(4)查询学生信息

3.定义一个类`MainClass`,包含如下功能:

(1)实例化`StudentSystem`类

(2)通过`StudentSystem`类实现对学生信息的添加、修改、删除和查询操作

以下是代码实现:


public class Student {

  private String name;

  private int age;

  private int score;

  public Student(String name, int age, int score)

    this.name = name;

    this.age = age;

    this.score = score;

  

  public String getName()

    return name;

  

  public void setName(String name)

    this.name = name;

  

  public int getAge()

    return age;

  

  public void setAge(int age)

    this.age = age;

  

  public int getScore()

    return score;

  

  public void setScore(int score)

    this.score = score;

  

}


import java.util.ArrayList;

import java.util.List;

public class StudentSystem {

  private List studentList;

  public StudentSystem() {

    studentList = new ArrayList ();

  }

  public boolean addStudent(Student student) {

    return studentList.add(student);

  }

  public boolean modifyStudent(Student student) {

    int index = studentList.indexOf(student);

    if (index != -1) {

      studentList.set(index, student);

      return true;

    }

    return false;

  }

  public boolean deleteStudent(Student student) {

    return studentList.remove(student);

  }

  public List queryStudentByName(String name) {

    List list = new ArrayList ();

    for (Student student : studentList) {

      if (student.getName().equals(name)) {

        list.add(student);

      }

    }

    return list;

  }

  public List queryStudentByScore(int score) {

    List list = new ArrayList ();

    for (Student student : studentList) {

      if (student.getScore() == score) {

        list.add(student);

      }

    }

    return list;

  }

}


import java.util.List;

import java.util.Scanner;

public class MainClass {

  private static StudentSystem studentSystem;

  public static void main(String[] args) {

    studentSystem = new StudentSystem();

    showMenu();

  }

  private static void showMenu() {

    System.out.println("请选择操作:");

    System.out.println("1、添加学生信息");

    System.out.println("2、删除学生信息");

    System.out.println("3、修改学生信息");

    System.out.println("4、查询学生信息");

    System.out.println("5、退出");

    Scanner scanner = new Scanner(System.in);

    int choice = scanner.nextInt();

    switch (choice) {

      case 1:

        addStudent();

        break;

      case 2:

        deleteStudent();

        break;

      case 3:

        modifyStudent();

        break;

      case 4:

        queryStudent();

        break;

      case 5:

        System.exit(0);

        break;

      default:

        System.out.println("请输入正确的选项");

        showMenu();

        break;

    }

  }

  private static void addStudent() {

    System.out.println("请输入学生姓名:");

    Scanner scanner = new Scanner(System.in);

    String name = scanner.next();

    System.out.println("请输入学生年龄:");

    int age = scanner.nextInt();

    System.out.println("请输入学生成绩:");

    int score = scanner.nextInt();

    Student student = new Student(name, age, score);

    boolean result = studentSystem.addStudent(student);

    if (result) {

      System.out.println("添加成功");

    } else {

      System.out.println("添加失败");

    }

    showMenu();

  }

  private static void deleteStudent() {

    System.out.println("请输入学生姓名:");

    Scanner scanner = new Scanner(System.in);

    String name = scanner.next();

    System.out.println("请输入学生成绩:");

    int score = scanner.nextInt();

    List resultList = studentSystem.queryStudentByName(name);

    for (Student student : resultList) {

      if (student.getScore() == score) {

        boolean result = studentSystem.deleteStudent(student);

        if (result) {

          System.out.println("删除成功");

        } else {

          System.out.println("删除失败");

        }

      }

    }

    showMenu();

  }

  private static void modifyStudent() {

    System.out.println("请输入学生姓名:");

    Scanner scanner = new Scanner(System.in);

    String name = scanner.next();

    System.out.println("请输入学生成绩:");

    int score = scanner.nextInt();

    List resultList = studentSystem.queryStudentByName(name);

    for (Student student : resultList) {

      if (student.getScore() == score) {

        System.out.println("请输入学生新姓名:");

        String newName = scanner.next();

        System.out.println("请输入学生新年龄:");

        int newAge = scanner.nextInt();

        System.out.println("请输入学生新成绩:");

        int newScore = scanner.nextInt();

        student.setName(newName);

        student.setAge(newAge);

        student.setScore(newScore);

        boolean result = studentSystem.modifyStudent(student);

        if (result) {

          System.out.println("修改成功");

        } else {

          System.out.println("修改失败");

        }

      }

    }

    showMenu();

  }

  private static void queryStudent() {

    System.out.println("请选择查询方式:");

    System.out.println("1、按姓名查询");

    System.out.println("2、按成绩查询");

    Scanner scanner = new Scanner(System.in);

    int choice = scanner.nextInt();

    switch (choice) {

      case 1:

        queryStudentByName();

        break;

      case 2:

        queryStudentByScore();

        break;

      default:

        System.out.println("请输入正确的选项");

        queryStudent();

        break;

    }

  }

  private static void queryStudentByName() {

    System.out.println("请输入学生姓名:");

    Scanner scanner = new Scanner(System.in);

    String name = scanner.next();

    List resultList = studentSystem.queryStudentByName(name);

    if (resultList.isEmpty()) {

      System.out.println("未查询到符合条件的学生");

    } else {

      for (Student student : resultList) {

        System.out.println(student.getName() + '\t' + student.getAge() + '\t' + student.getScore());

      }

    }

    showMenu();

  }

  private static void queryStudentByScore() {

    System.out.println("请输入学生成绩:");

    Scanner scanner = new Scanner(System.in);

    int score = scanner.nextInt();

    List resultList = studentSystem.queryStudentByScore(score);

    if (resultList.isEmpty()) {

      System.out.println("未查询到符合条件的学生");

    } else {

      for (Student student : resultList) {

        System.out.println(student.getName() + '\t' + student.getAge() + '\t' + student.getScore());

      }

    }

    showMenu();

  }

}

本文实现的成绩管理系统,可根据用户需求对学生信息进行添加、修改、删除和查询操作,方便高校教师和学生进行成绩管理。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复
    相似文章