使用对象数组实现学生管理系统
1 package day04test; 2 import com.sun.org.apache.bcel.internal.classfile.Code; 3 import day04_2.Stu; 4 5 import java.util.Scanner; 6 7 /** 8 * @author liuwenlong 9 * @create 2020-06-30 17:28:04 10 */ 11 public class TestStuMgr { 12 13 public static Scanner input = new Scanner(System.in); 14 //记录插入学生的总数量 15 public static int i = 0; 16 17 18 19 public static void main(String[] args) { 20 21 boolean isLogin = false; 22 do{ 23 System.out.println("==================欢迎进入学生管理系统后台================="); 24 System.out.println("账号:"); 25 String myuserName = input.next(); 26 System.out.println("密码:"); 27 String myPassWord = input.next(); 28 29 System.out.println("请输入验证码:"); 30 //获取验证码 31 String verificationCode = CodeUtil.genCode(); 32 System.out.println("验证码:"+verificationCode); 33 String myVerificationCode = input.next(); 34 35 User user = new User(); 36 if (user.getUserName().equals(myuserName) &&user.getPassword().equals(myPassWord) 37 &&verificationCode.equalsIgnoreCase(myVerificationCode)){ 38 //定义10学生的数组 39 System.out.println("登录成功"); 40 Student[] stu = new Student[10]; 41 boolean isExit = true; //判断是否结束 42 43 do { 44 System.out.println("=====================学生管理系统====================="); 45 System.out.println("【1】 添加学生"); 46 System.out.println("【2】 修改学生"); 47 System.out.println("【3】 删除学生"); 48 System.out.println("【4】 显示学生"); 49 System.out.println("【5】 分数统计"); 50 System.out.println("【6】 退出系统"); 51 52 System.out.println("请输入相应功能(序号):"); 53 int option = input.nextInt(); 54 switch (option) { 55 case 1: 56 StuService.addStudent(stu); 57 break; 58 case 2: 59 StuService.updateStudent(stu); 60 break; 61 case 3: 62 StuService.delByStuNo(stu); 63 break; 64 case 4: 65 StuService.printfStuInfo(stu); 66 break; 67 case 5: 68 StuService.printScoreInfo(stu); 69 break; 70 case 6: 71 System.out.println("确认退出吗?(y/n)"); //退出 72 String optionExit = input.next(); 73 if ("y".equalsIgnoreCase(optionExit)) { 74 isExit = false; 75 System.out.println("退出"); 76 } else { 77 System.out.println("您取消了退出!"); 78 } 79 break; 80 default: //输入有误 81 System.out.println("输入有误,请重新输入[1-6]"); 82 } 83 } while (isExit); 84 }else { 85 //账号获密码错误 继续循环 输入密码 86 isLogin=true; 87 } 88 }while (isLogin); 89 90 } 91 92 }
1 package day04test; 2 3 /** 4 * @author liuwenlong 5 * @create 2020-06-30 17:28:35 6 */ 7 public class Student { 8 private String stuNo; 9 private String name; 10 private int score; 11 private String pwd; 12 13 public Student() { 14 } 15 16 public Student(String stuNo, String name, int score, String pwd) { 17 this.stuNo = stuNo; 18 this.name = name; 19 this.score = score; 20 this.pwd = pwd; 21 } 22 23 public String getStuNo() { 24 return stuNo; 25 } 26 27 public void setStuNo(String stuNo) { 28 this.stuNo = stuNo; 29 } 30 31 public String getName() { 32 return name; 33 } 34 35 public void setName(String name) { 36 this.name = name; 37 } 38 39 public int getScore() { 40 return score; 41 } 42 43 public void setScore(int score) { 44 this.score = score; 45 } 46 47 public String getPwd() { 48 return pwd; 49 } 50 51 public void setPwd(String pwd) { 52 this.pwd = pwd; 53 } 54 55 //打印信息 56 public void showInfo(){ 57 System.out.println("学号:"+this.stuNo+",姓名:"+this.name+",成绩:"+this.score); 58 } 59 }
1 package day04test; 2 3 import day04_2.Stu; 4 5 import java.util.Arrays; 6 import java.util.Scanner; 7 8 /** 9 * @author liuwenlong 10 * @create 2020-06-30 17:27:42 11 */ 12 public class StuService { 13 public static Scanner input = new Scanner(System.in); 14 15 /** 16 * 显示学生信息 17 */ 18 public static void printStuInfo(Student[] stu) { 19 System.out.println("序号\t\t学号\t\t名字\t\t分数"); 20 System.out.println("-----------------------------------------------"); 21 for (int j = 0; j < stu.length; j++) { 22 if (stu[j] == null) { 23 continue; 24 } 25 System.out.println((j + 1) + "\t\t\t" + stu[j].getStuNo() + "\t\t" + stu[j].getName() + "\t\t" + stu[j].getScore() + ""); 26 } 27 } 28 29 30 /** 31 * 添加学生 32 */ 33 public static void addStudent(Student[] stu) { 34 35 System.out.println("=======================添加学生====================="); 36 System.out.println("请输入学生的学号:"); 37 String myStuNo = input.next(); 38 39 System.out.println("请输入学生的姓名:"); 40 String myName = input.next(); 41 42 System.out.println("请输入学生的成绩:"); 43 int myScore = input.nextInt(); 44 45 System.out.println("请输入学生的密码:"); 46 String myPwd = input.next(); 47 48 Student s1 = new Student(myStuNo, myName, myScore, myPwd); 49 stu[TestStuMgr.i] = s1; 50 TestStuMgr.i++; 51 } 52 53 54 /** 55 * 修改学生 56 */ 57 public static void updateStudent(Student[] stu) { 58 System.out.println("请输入要修改的学生的学号:"); 59 String myUpdateStuNo = input.next(); 60 for (int k = 0; k < stu.length - 1; k++) { 61 if (stu[k].getStuNo().equals(myUpdateStuNo)) { 62 System.out.println("请重新输入【" + stu[k].getName() + "】同学的成绩:"); 63 int myUpdateScore = input.nextInt(); 64 stu[k].setScore(myUpdateScore); 65 66 System.out.println("请重新输入【" + stu[k].getName() + "】同学的密码:"); 67 String myUpdatePwd = input.next(); 68 stu[k].setPwd(myUpdatePwd); 69 //修改完毕后直接退出 70 return; 71 } 72 } 73 } 74 75 76 /** 77 * 删除学生 78 */ 79 public static void delByStuNo(Student[] stu) { 80 System.out.println("------------------------------------------------"); 81 System.out.println("请输入要删除的学号:"); 82 String DelStuNo = input.next(); //要删除的学号 83 84 85 for (int k = 0; k < TestStuMgr.i; k++) { 86 //遍历数组,查找和输入的学号相同的记录下来,然后删除此内容 87 //定位到当前值,然后从后往前覆盖,最后面的一个给一个空值,然后i-1 88 if (stu[k].getStuNo().equals(DelStuNo)) { 89 int j; 90 for (j = k; j < TestStuMgr.i - 1; j++) { 91 //后一项赋给前一项,把要删除的那一列覆盖 92 stu[j] = stu[(j + 1)]; 93 } 94 stu[TestStuMgr.i - 1] = null; 95 TestStuMgr.i--; 96 } 97 } 98 System.out.println("删除成功!"); 99 } 100 101 102 /** 103 * 显示学生信息 104 */ 105 public static void printfStuInfo(Student[] stu) { 106 //先判断是否为空 107 if (stu !=null&&stu.length>0){ 108 System.out.println("序号\t\t学号\t\t名字\t\t分数\t\t名次"); 109 System.out.println("--------------------------------------------------------"); 110 mySort(stu);//使用自定义排序 进行对数组排序---- 111 for (int j = 0; j < TestStuMgr.i; j++) { 112 if (stu[j] == null) { 113 continue; 114 } 115 System.out.println((j + 1) + "\t\t\t" + stu[j].getStuNo() + "\t\t" + stu[j].getName() + "\t\t" + stu[j].getScore() + "\t\t\t第" + (j + 1) + "名"); 116 } 117 } 118 119 } 120 121 122 /** 123 * 分数统计 124 */ 125 public static void printScoreInfo(Student[] stu) { 126 int maxScore = stu[0].getScore(); 127 int minScore = stu[0].getScore(); 128 int sumScore = stu[0].getScore(); 129 int maxIndex = 0; 130 int minIndex = 0; 131 double avgScore = 0; //统计平均分 132 for (int j = 1; j < TestStuMgr.i; j++) { 133 sumScore += stu[j].getScore(); 134 if (stu[j].getScore() > maxScore) { 135 maxScore = stu[j].getScore(); 136 maxIndex = j; 137 } 138 if (stu[j].getScore() < minScore) { 139 minScore = stu[j].getScore(); 140 minIndex = j; 141 } 142 } 143 144 avgScore = (sumScore / (TestStuMgr.i)); 145 System.out.println("最高分\t\t\t最低分\t\t\t平均分"); 146 System.out.println("---------------------------------------------"); 147 System.out.println(" " + maxScore + "(" + stu[maxIndex].getName() + ")" + "\t\t" + minScore + "(" + stu[minIndex].getName() + ")" + "\t\t" + avgScore + "\n\n"); 148 } 149 150 //根据分数降序 151 public static void mySort(Student[] stu) { 152 int i, j; 153 Student temp[] = new Student[10]; 154 for (i = 0; i < TestStuMgr.i - 1; i++) { 155 for (j = 0; j < TestStuMgr.i - i - 1; j++) { 156 //使用冒泡对成绩排序 157 if (stu[j].getScore() < stu[j + 1].getScore()) { 158 temp[j] = stu[j]; 159 stu[j] = stu[j + 1]; 160 stu[j + 1] = temp[j]; 161 } 162 } 163 } 164 } 165 166 167 }
1 package day04test; 2 3 /** 4 * 生成验证码 5 * @author liuwenlong 6 * @create 2020-06-30 17:27:54 7 */ 8 public class CodeUtil { 9 public static String genCode(){ 10 char arr[] = new char[4]; 11 int i = 0; 12 while (true) { 13 char ch = (char) (int) (Math.random() * 124); 14 if (ch >= 'A' && ch <= 'Z') { 15 arr[i++] = ch; 16 } 17 if (ch >= 'a' && ch <= 'z') { 18 arr[i++] = ch; 19 } 20 if (ch >= '0' && ch <= '9') { 21 arr[i++] = ch; 22 } 23 24 if (i == 4) { 25 break; 26 } 27 } 28 29 //将数组转为字符串 30 String yzm = new String(arr); 31 return yzm; 32 } 33 }
1 package day04test; 2 3 /**存放用户名和密码 4 * @author liuwenlong 5 * @create 2020-06-30 17:28:29 6 */ 7 public class User { 8 private String userName="admin"; 9 private String password="admin"; 10 11 public User() { 12 } 13 14 public User(String userName, String password) { 15 this.userName = userName; 16 this.password = password; 17 } 18 19 public String getUserName() { 20 return userName; 21 } 22 23 public void setUserName(String userName) { 24 this.userName = userName; 25 } 26 27 public String getPassword() { 28 return password; 29 } 30 31 public void setPassword(String password) { 32 this.password = password; 33 } 34 }
运行截图:
账号、密码、验证码(不区分大小写)
二、插入学生信息
三、查看学生信息、按照成绩降序(排名)
四、修改
五、删除
六、分数统计
原创文章,转载请说明出处,谢谢合作
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律