Java学生管理系统项目
生活随笔
收集整理的這篇文章主要介紹了
Java学生管理系统项目
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java學生管理系統項目
主界面展示圖:
首先先定義一個學生類;
主程序:
import java.util.ArrayList; import java.util.Scanner;public class StudentManager {//主界面系統public static void main(String[] args) {ArrayList<Student> array = new ArrayList<Student>();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.退出");System.out.println("請輸入你的選擇:");Scanner sc = new Scanner(System.in);String line = sc.nextLine();switch (line) {case "1": // System.out.println("修改學生");addStudent(array);break;case "2": // System.out.println("刪除學生");deleteStudent(array);break;case "3": // System.out.println("修改學生");updateStudent(array);break;case "4": // System.out.println("查看所有學生");findAllStudent(array);break;case "5":System.out.println("歡迎使用!");System.exit(0);//關閉虛擬機}}}//添加學生系統public static void addStudent(ArrayList<Student> array) {Scanner sc = new Scanner(System.in);String sid;//學號重復報錯機制while (true) {System.out.println("請輸入學生的學號:");sid = sc.nextLine();boolean flag = isUsed(array, sid);if (flag) {System.out.println("你輸入的學號重復,請重新輸入!");} else {break;}}System.out.println("請輸入學生的姓名:");String name = sc.nextLine();System.out.println("請輸入學生的年齡:");String age = sc.nextLine();System.out.println("請輸入學生的居住地:");String address = sc.nextLine();Student s = new Student(sid, name, age, address);//創建學生對象array.add(s);//將學生對象添加到集合中System.out.println("添加成功!");}//學號重復機制public static boolean isUsed(ArrayList<Student> array, String sid) {boolean flag = false;for (int i = 0; i < array.size(); i++) {Student s1 = array.get(i);if (s1.getSid().equals(sid)) {//如果你輸入的學號跟之前輸入的學號重復直接提示報錯flag = true;//如果flag變量被賦值為true,就說明學號已經被使用}}return flag;}//刪除學生系統public static void deleteStudent(ArrayList<Student> array) {Scanner sc = new Scanner(System.in);System.out.println("請輸入你要刪除的學生的學號:");String sid = sc.nextLine();for (int i = 0; i < array.size(); i++) {Student s = array.get(i);if (s.getSid().equals(sid)) {//對比學生的學號成員變量與輸入的學號是否相同array.remove(i);System.out.println(s.getName() + "同學已被刪除成功!");return;}}System.out.println("沒有找到你要刪除的學生!!!");//輸錯學號找不到學生機制}//修改學生public static void updateStudent(ArrayList<Student> array) {Scanner sc = new Scanner(System.in);System.out.println("請輸入你要修改的學生的學號:");String sid = sc.nextLine();for (int i = 0; i < array.size(); i++) {Student s = array.get(i);if (s.getSid().equals(sid)) {System.out.println("請輸入學生的新姓名:");String name = sc.nextLine();System.out.println("請輸入學生的新年齡:");String age = sc.nextLine();System.out.println("請輸入學生的新居住地:");String address = sc.nextLine();Student a = new Student();a.setSid(sid);a.setName(name);a.setAge(age);a.setAddress(address);array.set(i, a);//將新的學生信息交給集合System.out.println("修改學生成功!");return;}}System.out.println("未找到要修改的學生!");//找不到要修改的學生機制}//查看所有學生public static void findAllStudent(ArrayList<Student> array) {if (array.size() == 0) {//無學生信息機制System.out.println("沒有學生的信息,請先添加學生!");return;}System.out.println("學號" + " " + "姓名" + " " + "年齡" + " " + "居住地");for (int i = 0; i < array.size(); i++) {Student s = array.get(i);System.out.println(s.getSid() + " \t" + s.getName() + " \t" + s.getAge() + "歲" + " \t" + s.getAddress());}}}總結
以上是生活随笔為你收集整理的Java学生管理系统项目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Delphi控件的“拿来主义”
- 下一篇: 判断操作系统的位数