用Java写学生成绩管理系统,需要250行代码,并附上详细解释
时间: 2024-03-21 18:40:14 浏览: 19
学生成绩管理系统是一个比较典型的基于控制台的应用程序,主要功能是管理学生的基本信息和成绩信息。本文将介绍如何用Java编写一个具有基本功能的学生成绩管理系统,代码行数约为250行。
在Java中,我们可以使用面向对象的思想来编写学生成绩管理系统,主要分为以下几个步骤:
1. 定义学生类和成绩类
首先,我们需要定义学生类和成绩类,这两个类是整个系统的核心。学生类包括学生的基本信息,如姓名、学号、性别、年龄等等,成绩类包括学生的各科成绩信息,如数学、语文、英语等等。
代码如下:
```java
// 学生类
public class Student {
private String name; // 姓名
private String id; // 学号
private String gender; // 性别
private int age; // 年龄
public Student(String name, String id, String gender, int age) {
this.name = name;
this.id = id;
this.gender = gender;
this.age = age;
}
// getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
// 成绩类
public class Score {
private int math; // 数学成绩
private int chinese; // 语文成绩
private int english; // 英语成绩
public Score(int math, int chinese, int english) {
this.math = math;
this.chinese = chinese;
this.english = english;
}
// getters and setters
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
}
```
2. 定义数据存储类
接下来,我们需要定义一个数据存储类,用来存储学生信息和成绩信息。在本例中,我们使用了一个简单的数组来存储学生和成绩。当然,实际应用中,我们可以使用数据库或文件来存储数据。
代码如下:
```java
public class Data {
private Student[] students; // 学生数组
private Score[] scores; // 成绩数组
private int count; // 学生计数器
public Data() {
students = new Student[100];
scores = new Score[100];
count = 0;
}
// 添加学生信息
public void addStudent(Student student) {
students[count] = student;
count++;
}
// 添加成绩信息
public void addScore(Score score) {
scores[count - 1] = score;
}
// 根据学号查询学生信息
public Student findStudentById(String id) {
for (int i = 0; i < count; i++) {
if (students[i].getId().equals(id)) {
return students[i];
}
}
return null;
}
// 根据学号查询成绩信息
public Score findScoreById(String id) {
for (int i = 0; i < count; i++) {
if (students[i].getId().equals(id)) {
return scores[i];
}
}
return null;
}
}
```
3. 定义主程序类
最后,我们需要定义一个主程序类来实现学生成绩管理系统的基本功能。在本例中,我们实现了添加学生信息、添加成绩信息、查询学生信息、查询成绩信息等功能。
代码如下:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Data data = new Data();
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 添加学生信息");
System.out.println("2. 添加成绩信息");
System.out.println("3. 查询学生信息");
System.out.println("4. 查询成绩信息");
System.out.println("5. 退出系统");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("请输入学生姓名:");
String name = scanner.next();
System.out.print("请输入学生学号:");
String id = scanner.next();
System.out.print("请输入学生性别:");
String gender = scanner.next();
System.out.print("请输入学生年龄:");
int age = scanner.nextInt();
Student student = new Student(name, id, gender, age);
data.addStudent(student);
System.out.println("添加学生信息成功!");
break;
case 2:
System.out.print("请输入学生学号:");
String id2 = scanner.next();
System.out.print("请输入数学成绩:");
int math = scanner.nextInt();
System.out.print("请输入语文成绩:");
int chinese = scanner.nextInt();
System.out.print("请输入英语成绩:");
int english = scanner.nextInt();
Score score = new Score(math, chinese, english);
data.addScore(score);
System.out.println("添加成绩信息成功!");
break;
case 3:
System.out.print("请输入学生学号:");
String id3 = scanner.next();
Student student1 = data.findStudentById(id3);
if (student1 == null) {
System.out.println("该学生不存在!");
} else {
System.out.println("姓名:" + student1.getName());
System.out.println("学号:" + student1.getId());
System.out.println("性别:" + student1.getGender());
System.out.println("年龄:" + student1.getAge());
}
break;
case 4:
System.out.print("请输入学生学号:");
String id4 = scanner.next();
Score score1 = data.findScoreById(id4);
if (score1 == null) {
System.out.println("该学生不存在!");
} else {
System.out.println("数学成绩:" + score1.getMath());
System.out.println("语文成绩:" + score1.getChinese());
System.out.println("英语成绩:" + score1.getEnglish());
}
break;
case 5:
System.out.println("感谢使用学生成绩管理系统!");
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
}
```
以上就是一个简单的学生成绩管理系统的实现。当然,这只是一个基础的框架,如果要实现更多功能,需要根据实际需求进行扩展。