只写了最基本的增删改查,其中可能会有一些没考虑到的地方。结尾有完整代码
首先是main方法
public static void main(String[] args) throws Exception {
//创建Scanner对象和ArrayList集合
Scanner sc = new Scanner(System.in);
ArrayList<Student> students = new ArrayList<>();
//放入死循环能一直运行
while (true) {
Welcome();
Menu(sc, students);
}
}
然后是菜单和欢迎页面
//欢迎界面
private static void Welcome() {
System.out.println("请选择您要进行的操作:");
System.out.println("1.录入学生信息");
System.out.println("2.删除学生信息");
System.out.println("3.修改学生信息");
System.out.println("4.查询学生信息");
System.out.println("5.展示所有学生成绩信息");
System.out.println("0.退出系统");
}
//真正的菜单
private static void Menu(Scanner sc, ArrayList<Student> students) {
switch (sc.next()) {
case "1" -> add(sc, students); //添加学生信息
case "2" -> deleteMessage(sc, students); //删除学生信息
case "3" -> change(sc, students); //修改学生信息
case "4" -> Search(sc, students); //查询学生信息
case "5" -> show(students); //列出所有学生的成绩
case "0" -> System.exit(0);
}
}
1:添加学生信息
private static void add(Scanner sc, ArrayList<Student> students) {
Student s = new Student();
StudentMessage(sc, s);
students.add(s);
}
2:删除学生信息
//"2"->删除学生信息
private static void deleteMessage(Scanner sc, ArrayList<Student> students) {
if (judge(students)) {
System.out.println("请输入要删除学生的姓名");
String next = sc.next();
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
if (student.getName().equals(next)) {
System.out.println("学号\t\t姓名\t\t性别\t\t年龄\t\t语文\t\t数学\t\t英语");
showMessage(student);
System.out.println("确定要删除吗?Y/N");
if (sc.next().equalsIgnoreCase("Y")) {
students.remove(i);
System.out.println("删除成功!");
} else {
System.out.println("删除失败!退回菜单页");
}
return;
}
}
System.out.println("没有找到该学生!");
} else {
System.out.println("操作失败!");
}
}
3:修改学生信息
//"3"->修改学生信息
private static void change(Scanner sc, ArrayList<Student> students) {
if (judge(students)) {
System.out.println("请输入要修改信息的学生姓名");
String n = sc.next();
for (Student student : students) {
if (student.getName().equals(n)) {
StudentMessage(sc, student);
return;
}
}
} else {
System.out.println("操作失败");
}
}
4:查询单个学生信息
// "4"->查询单个学生信息
private static void Search(Scanner sc, ArrayList<Student> students) {
if (judge(students)) {
System.out.println("请输入要查询的学生姓名");
String next = sc.next();
for (Student student : students) {
if (student.getName().equals(next)) {
System.out.println("学号\t\t姓名\t\t性别\t\t年龄\t\t语文\t\t数学\t\t英语");
showMessage(student);
return;
}
}
System.out.println("没有找到该学生的信息!");
} else {
System.out.println("操作失败!");
}
}
5:列出所有学生信息
// "5"->列出所有学生
private static void show(ArrayList<Student> students) {
if (judge(students)) {
System.out.println("学号\t\t姓名\t\t性别\t\t年龄\t\t语文\t\t数学\t\t英语");
for (Student student : students) {
showMessage(student);
}
} else {
System.out.println("操作失败!");
}
}
用到的其他方法:
private static void StudentMessage(Scanner sc, Student s) {
System.out.println("请输入学生的学号:");
s.setId(sc.next());
System.out.println("请输入学生的姓名:");
s.setName(sc.next());
System.out.println("请输入学生的年龄:");
s.setAge(sc.nextInt());
while (true) {
System.out.println("请输入学生的性别:");
String next = sc.next();
if (next.equals("女") || next.equals("男")) {
s.setGender(next);
break;
} else {
System.out.println("性别只有男女,没有你说的这个,请重新输入");
}
}
while (true) {
System.out.println("请输入学生的语文成绩:");
double v = scanScore(sc);
if (v != -1) {
s.setChinese_score(v);
break;
}
}
while (true) {
System.out.println("请输入学生的数学成绩:");
double v = scanScore(sc);
if (v != -1) {
s.setMath_score(v);
break;
}
}
while (true) {
System.out.println("请输入学生的英语成绩:");
double v = scanScore(sc);
if (v != -1) {
s.setEnglish_score(v);
break;
}
}
}
//根据Student对象展示单个学生的信息
private static void showMessage(Student student) {
System.out.println(student.getId() + "\t\t" + student.getName() + "\t\t" + student.getGender() +
"\t\t" + student.getAge() + "\t\t" + student.getChinese_score() + "\t\t" + student.getMath_score() +
"\t\t" + student.getEnglish_score());
}
//判断系统中是否有学生信息
private static boolean judge(ArrayList<Student> students) {
if (students.size() == 0) {
System.out.println("还没有学生信息呢,请添加后再进行后续操作");
return false;
}
return true;
}
private static double scanScore(Scanner sc) {
double v = sc.nextDouble();
if (v > 100 || v < 0) {
System.out.println("输入成绩不在允许范围内,请重新输入");
return -1;
}
return v;
}
完整发一下代码:
学生类:
public class Student {
private String id;
private String name;
private int age;
private String gender;
private double Math_score;
private double Chinese_score;
private double English_score;
public Student() {
}
public Student(String id, String name, int age, String gender, double math_score, double chinese_score, double english_score) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
Math_score = math_score;
Chinese_score = chinese_score;
English_score = english_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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public double getMath_score() {
return Math_score;
}
public void setMath_score(double math_score) {
Math_score = math_score;
}
public double getChinese_score() {
return Chinese_score;
}
public void setChinese_score(double chinese_score) {
Chinese_score = chinese_score;
}
public double getEnglish_score() {
return English_score;
}
public void setEnglish_score(double english_score) {
English_score = english_score;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
StudentSystem类:
import java.util.ArrayList;
import java.util.Scanner;
//学生信息管理系统
//功能:增、删、改、查
public class StudentSystem {
public static void main(String[] args) throws Exception {
//创建Scanner对象和ArrayList集合
Scanner sc = new Scanner(System.in);
ArrayList<Student> students = new ArrayList<>();
//放入死循环能一直运行
while (true) {
Welcome();
Menu(sc, students);
}
}
//欢迎界面
private static void Welcome() {
System.out.println("请选择您要进行的操作:");
System.out.println("1.录入学生信息");
System.out.println("2.删除学生信息");
System.out.println("3.修改学生信息");
System.out.println("4.查询学生信息");
System.out.println("5.展示所有学生成绩信息");
System.out.println("0.退出系统");
}
//真正的菜单
private static void Menu(Scanner sc, ArrayList<Student> students) {
switch (sc.next()) {
case "1" -> add(sc, students); //添加学生信息
case "2" -> deleteMessage(sc, students); //删除学生信息
case "3" -> change(sc, students); //修改学生信息
case "4" -> Search(sc, students); //查询学生信息
case "5" -> show(students); //列出所有学生的成绩
case "0" -> System.exit(0);
}
}
// "1"->添加学生信息
private static void add(Scanner sc, ArrayList<Student> students) {
Student s = new Student();
StudentMessage(sc, s);
students.add(s);
}
//"2"->删除学生信息
private static void deleteMessage(Scanner sc, ArrayList<Student> students) {
if (judge(students)) {
System.out.println("请输入要删除学生的姓名");
String next = sc.next();
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
if (student.getName().equals(next)) {
System.out.println("学号\t\t姓名\t\t性别\t\t年龄\t\t语文\t\t数学\t\t英语");
showMessage(student);
System.out.println("确定要删除吗?Y/N");
if (sc.next().equalsIgnoreCase("Y")) {
students.remove(i);
System.out.println("删除成功!");
} else {
System.out.println("删除失败!退回菜单页");
}
return;
}
}
System.out.println("没有找到该学生!");
} else {
System.out.println("操作失败!");
}
}
//"3"->修改学生信息
private static void change(Scanner sc, ArrayList<Student> students) {
if (judge(students)) {
System.out.println("请输入要修改信息的学生姓名");
String n = sc.next();
for (Student student : students) {
if (student.getName().equals(n)) {
StudentMessage(sc, student);
return;
}
}
} else {
System.out.println("操作失败");
}
}
// "4"->查询单个学生信息
private static void Search(Scanner sc, ArrayList<Student> students) {
if (judge(students)) {
System.out.println("请输入要查询的学生姓名");
String next = sc.next();
for (Student student : students) {
if (student.getName().equals(next)) {
System.out.println("学号\t\t姓名\t\t性别\t\t年龄\t\t语文\t\t数学\t\t英语");
showMessage(student);
return;
}
}
System.out.println("没有找到该学生的信息!");
} else {
System.out.println("操作失败!");
}
}
// "5"->列出所有学生
private static void show(ArrayList<Student> students) {
if (judge(students)) {
System.out.println("学号\t\t姓名\t\t性别\t\t年龄\t\t语文\t\t数学\t\t英语");
for (Student student : students) {
showMessage(student);
}
} else {
System.out.println("操作失败!");
}
}
private static void StudentMessage(Scanner sc, Student s) {
System.out.println("请输入学生的学号:");
s.setId(sc.next());
System.out.println("请输入学生的姓名:");
s.setName(sc.next());
System.out.println("请输入学生的年龄:");
s.setAge(sc.nextInt());
while (true) {
System.out.println("请输入学生的性别:");
String next = sc.next();
if (next.equals("女") || next.equals("男")) {
s.setGender(next);
break;
} else {
System.out.println("性别只有男女,没有你说的这个,请重新输入");
}
}
while (true) {
System.out.println("请输入学生的语文成绩:");
double v = scanScore(sc);
if (v != -1) {
s.setChinese_score(v);
break;
}
}
while (true) {
System.out.println("请输入学生的数学成绩:");
double v = scanScore(sc);
if (v != -1) {
s.setMath_score(v);
break;
}
}
while (true) {
System.out.println("请输入学生的英语成绩:");
double v = scanScore(sc);
if (v != -1) {
s.setEnglish_score(v);
break;
}
}
}
//根据Student对象展示单个学生的信息
private static void showMessage(Student student) {
System.out.println(student.getId() + "\t\t" + student.getName() + "\t\t" + student.getGender() +
"\t\t" + student.getAge() + "\t\t" + student.getChinese_score() + "\t\t" + student.getMath_score() +
"\t\t" + student.getEnglish_score());
}
//判断系统中是否有学生信息
private static boolean judge(ArrayList<Student> students) {
if (students.size() == 0) {
System.out.println("还没有学生信息呢,请添加后再进行后续操作");
return false;
}
return true;
}
private static double scanScore(Scanner sc) {
double v = sc.nextDouble();
if (v > 100 || v < 0) {
System.out.println("输入成绩不在允许范围内,请重新输入");
return -1;
}
return v;
}
}
这就是所有内容了,这篇文章可用于学校作业(*^_^*),但主要学习思路,我的代码也不是最简单的,其中有很多地方还可以简化,这只是我学习Java的练手作品,请酌情观看。
占位置的文字,用于消除改进质量的提醒文章来源地址https://www.toymoban.com/news/detail-531450.html
占位置的文字,用于消除改进质量的提醒文章来源:https://www.toymoban.com/news/detail-531450.html
占位置的文字,用于消除改进质量的提醒
占位置的文字,用于消除改进质量的提醒
占位置的文字,用于消除改进质量的提醒
占位置的文字,用于消除改进质量的提醒
占位置的文字,用于消除改进质量的提醒
占位置的文字,用于消除改进质量的提醒
占位置的文字,用于消除改进质量的提醒
占位置的文字,用于消除改进质量的提醒
到了这里,关于JAVA学生成绩管理系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!