用java设计一个简单的学生成绩系统,具有以下功能:添加一个学生;查找一个学生;根据编号更新学生的基本信息(基本信息为姓名与学号);根据编号删除学生;根据编号输入学生的各门成绩(语文、数学、外语,成绩为int类型);退出系统。
时间: 2023-11-22 10:55:57 浏览: 56
好的,下面是一个简单的学生成绩系统的代码示例,使用Java语言编写,具有添加、查找、更新、删除学生信息和输入学生成绩等基本功能:
```
import java.util.*;
public class StudentScoreSystem {
private static Map<String, Student> studentMap = new HashMap<String, Student>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("请选择操作:1.添加学生 2.查找学生 3.更新学生 4.删除学生 5.输入学生成绩 6.退出系统");
int input = scanner.nextInt();
scanner.nextLine(); //清空输入缓冲区
switch (input) {
case 1:
addStudent();
break;
case 2:
findStudent();
break;
case 3:
updateStudent();
break;
case 4:
deleteStudent();
break;
case 5:
inputScore();
break;
case 6:
System.out.println("已退出系统。");
System.exit(0);
break;
default:
System.out.println("输入错误,请重新选择。");
break;
}
}
}
//添加学生
private static void addStudent() {
System.out.println("请输入学生的姓名和学号,中间用空格分隔:");
String[] input = scanner.nextLine().split(" ");
if (input.length != 2) {
System.out.println("输入有误,请重新输入。");
return;
}
String name = input[0];
String id = input[1];
if (studentMap.containsKey(id)) {
System.out.println("该学号已存在,请重新输入。");
return;
}
studentMap.put(id, new Student(name, id));
System.out.println("添加成功!");
}
//查找学生
private static void findStudent() {
System.out.println("请输入要查找的学生的学号:");
String id = scanner.nextLine();
if (studentMap.containsKey(id)) {
System.out.println(studentMap.get(id));
} else {
System.out.println("没有找到该学生。");
}
}
//更新学生
private static void updateStudent() {
System.out.println("请输入要更新的学生的学号:");
String id = scanner.nextLine();
if (studentMap.containsKey(id)) {
System.out.println("请输入新的姓名和学号,中间用空格分隔:");
String[] input = scanner.nextLine().split(" ");
if (input.length != 2) {
System.out.println("输入有误,请重新输入。");
return;
}
String name = input[0];
String newId = input[1];
if (!id.equals(newId) && studentMap.containsKey(newId)) {
System.out.println("该学号已存在,请重新输入。");
return;
}
studentMap.get(id).setName(name);
studentMap.remove(id);
studentMap.put(newId, new Student(name, newId));
System.out.println("更新成功!");
} else {
System.out.println("没有找到该学生。");
}
}
//删除学生
private static void deleteStudent() {
System.out.println("请输入要删除的学生的学号:");
String id = scanner.nextLine();
if (studentMap.containsKey(id)) {
studentMap.remove(id);
System.out.println("删除成功!");
} else {
System.out.println("没有找到该学生。");
}
}
//输入学生成绩
private static void inputScore() {
System.out.println("请输入要输入成绩的学生的学号:");
String id = scanner.nextLine();
if (studentMap.containsKey(id)) {
System.out.println("请输入语文、数学、外语成绩,中间用空格分隔:");
String[] input = scanner.nextLine().split(" ");
if (input.length != 3) {
System.out.println("输入有误,请重新输入。");
return;
}
int chineseScore = Integer.parseInt(input[0]);
int mathScore = Integer.parseInt(input[1]);
int englishScore = Integer.parseInt(input[2]);
studentMap.get(id).setChineseScore(chineseScore);
studentMap.get(id).setMathScore(mathScore);
studentMap.get(id).setEnglishScore(englishScore);
System.out.println("输入成功!");
} else {
System.out.println("没有找到该学生。");
}
}
}
class Student {
private String name;
private String id;
private int chineseScore;
private int mathScore;
private int englishScore;
public Student(String name, String id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public int getChineseScore() {
return chineseScore;
}
public void setChineseScore(int chineseScore) {
this.chineseScore = chineseScore;
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int mathScore) {
this.mathScore = mathScore;
}
public int getEnglishScore() {
return englishScore;
}
public void setEnglishScore(int englishScore) {
this.englishScore = englishScore;
}
@Override
public String toString() {
return "姓名:" + name + "\n学号:" + id + "\n语文成绩:" + chineseScore + "\n数学成绩:" + mathScore + "\n外语成绩:" + englishScore;
}
}
```
上述代码实现了一个简单的学生成绩系统,包括添加、查找、更新、删除学生信息和输入学生成绩等基本功能。具体实现方式是使用Map来存储学生信息,其中键为学号,值为Student对象,Student类封装了学生的基本信息和成绩。通过输入不同的操作编号,可以进行相应的操作。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)