【超详细】Java实现学生信息管理系统
生活随笔
收集整理的這篇文章主要介紹了
【超详细】Java实现学生信息管理系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?項目介紹:用java實現學生信息的管理,其中錄入的數據包括:學號、姓名、年齡、居住地等,并且能夠實現對學生信息的添加、修改、刪除、查看功能。
一、創建項目
1、項目名稱:myStudentManager
二、創建包
1、包名稱:study
2、名字也可以自己進行命名
三、創建兩個類
1、學生類(Studnet)
package Study; /*學生類Alt + Insert 根據自己的需要進行選擇*/ public class Student {//學號private String sid;//姓名private String name;//年齡private String age;//居住地private String address;public Student() {}public Student(String sid, String name, String age, String address) {this.sid = sid;this.name = name;this.age = age;this.address = address;}public String getSid() {return sid;}public void setSid(String sid) {this.sid = sid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;} }2、學生測試類(StudentManager)
package Study;import jdk.swing.interop.SwingInterOpUtils;import java.util.ArrayList; import java.util.Scanner;/*學生管理系統*/ public class StudentManager {/*1:用輸出語句完成主界面的編寫2:用Scanner實現鍵盤錄入數據3:用switch語句完成操作的選擇4:用循環完成再次回到主界面*/public static void main(String[] args) {//創建一個集合對象用于儲存學生數據ArrayList<Student> array = new ArrayList<>();//用循環完成再次回到主界面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實現鍵盤錄入數據Scanner sc = new Scanner(System.in);String line = sc.nextLine();//用switch語句完成操作的選擇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("謝謝使用"); // break;System.exit(0); //JVM(java虛擬機)退出}}}//定義一個方法,用于添加學生信息public static void addStudent(ArrayList<Student> array) {//鍵盤錄入學生對象所需要的數據,顯示提示信息,提示要輸入何種信息Scanner sc = new Scanner(System.in);//為了能使得sid能在while循環外面被訪問到,我們就把他定義到了循環外面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();s.setSid(sid);s.setName(name);s.setAge(age);s.setAddress(address);//將學生對象添加到集合中array.add(s);//給出添加成功提示System.out.println("添加學生成功");}//定義一個方法,判斷學號是否被使用public static boolean isUsed(ArrayList<Student> array, String sid){//如果與集合中的某一學生學號相同,返回ture;如果都不相同,返回falseboolean flag = false;for (int i = 0; i < array.size(); i++){Student s = array.get(i);if (s.getSid().equals(sid)){flag = true;break;}}return flag;}//定義一個方法,用于查看學生信息public static void findAllStudent(ArrayList<Student> array) {//判斷集合中是否有數據,如果沒有顯示提示信息if (array.size() == 0) {System.out.println("無信息,請先添加信息再查詢");} else {//顯示表頭信息//\t 其實就是tab鍵的位置System.out.println("學號\t\t\t\t姓名\t\t年齡\t\t居住地");//將集合中的元素輸出顯示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());}}}//定義一個方法,用于刪除學生信息public static void deleteStudent(ArrayList<Student> array) {//鍵盤錄入要刪除的學生學號,顯示提示信息Scanner sc = new Scanner(System.in);System.out.println("請輸入你要刪除的學生學號:");String sid = sc.nextLine();//在刪除學生操作前,對學號是否存在進行判斷//如果不存在,顯示提示信息//遍歷集合將對應學生對象從集合中刪除int index = -1;for (int i = 0; i < array.size(); i++) {Student s = array.get(i);if (s.getSid().equals(sid)) { // array.remove(i);index = i;break;}}if (index == -1){System.out.println("該信息不存在,請重新輸入");} else{array.remove(index);//給出提示刪除學生成功System.out.println("刪除學生成功");}}//定義一個方法,用于修改學生信息public static void updateStudent(ArrayList<Student> array) {//鍵盤輸入要修改的學生學號,顯示提示信息Scanner sc = new Scanner(System.in);System.out.println("請輸入你要修改的學生學號:");String sid = sc.nextLine();int index = -1;//遍歷結合修改學生的對應信息for(int i = 0; i < array.size(); i++){Student student = array.get(i);if (student.getSid().equals(sid)){index = i; // array.set(i,s);break;}}if (index == -1) {System.out.println("不存在要修改的學生學號,請重新輸入");} else {//鍵盤錄入要修改的學生信息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();s.setSid(sid);s.setName(name);s.setAge(age);s.setAddress(address);array.set(index, s);//給出修改成功提示System.out.println("修改學生信息成功");}} }四、主要代碼解析
1、主界面的編寫
//用循環完成再次回到主界面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實現鍵盤錄入數據Scanner sc = new Scanner(System.in);String line = sc.nextLine();//用switch語句完成操作的選擇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("謝謝使用"); // break;System.exit(0); //JVM(java虛擬機)退出}}}?2、添加學生信息(解決重復添加問題)
public static void addStudent(ArrayList<Student> array) {//鍵盤錄入學生對象所需要的數據,顯示提示信息,提示要輸入何種信息Scanner sc = new Scanner(System.in);//為了能使得sid能在while循環外面被訪問到,我們就把他定義到了循環外面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();s.setSid(sid);s.setName(name);s.setAge(age);s.setAddress(address);//將學生對象添加到集合中array.add(s);//給出添加成功提示System.out.println("添加學生成功");}2.1、定義一個方法判斷學生學號是否被使用
public static boolean isUsed(ArrayList<Student> array, String sid){//如果與集合中的某一學生學號相同,返回ture;如果都不相同,返回falseboolean flag = false;for (int i = 0; i < array.size(); i++){Student s = array.get(i);if (s.getSid().equals(sid)){flag = true;break;}}return flag;}3、修改學生信息(解決修改學生不存在的問題)
public static void updateStudent(ArrayList<Student> array) {//鍵盤輸入要修改的學生學號,顯示提示信息Scanner sc = new Scanner(System.in);System.out.println("請輸入你要修改的學生學號:");String sid = sc.nextLine();int index = -1;//遍歷結合修改學生的對應信息for(int i = 0; i < array.size(); i++){Student student = array.get(i);if (student.getSid().equals(sid)){index = i; // array.set(i,s);break;}}if (index == -1) {System.out.println("不存在要修改的學生學號,請重新輸入");} else {//鍵盤錄入要修改的學生信息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();s.setSid(sid);s.setName(name);s.setAge(age);s.setAddress(address);array.set(index, s);//給出修改成功提示System.out.println("修改學生信息成功");}}4、刪除學生信息(解決刪除學生不存在的問題)
public static void deleteStudent(ArrayList<Student> array) {//鍵盤錄入要刪除的學生學號,顯示提示信息Scanner sc = new Scanner(System.in);System.out.println("請輸入你要刪除的學生學號:");String sid = sc.nextLine();//在刪除學生操作前,對學號是否存在進行判斷//如果不存在,顯示提示信息//遍歷集合將對應學生對象從集合中刪除int index = -1;for (int i = 0; i < array.size(); i++) {Student s = array.get(i);if (s.getSid().equals(sid)) { // array.remove(i);index = i;break;}}if (index == -1){System.out.println("該信息不存在,請重新輸入");} else{array.remove(index);//給出提示刪除學生成功System.out.println("刪除學生成功");}}5、查看所有學生
public static void findAllStudent(ArrayList<Student> array) {//判斷集合中是否有數據,如果沒有顯示提示信息if (array.size() == 0) {System.out.println("無信息,請先添加信息再查詢");} else {//顯示表頭信息//\t 其實就是tab鍵的位置System.out.println("學號\t\t\t\t姓名\t\t年齡\t\t居住地");//將集合中的元素輸出顯示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实现学生信息管理系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GD32F303串口ISP方式下载程序
- 下一篇: 阿里云超算集谛优化GPU异构并行性能:G