java学生管理系统(简单版)
簡單實現(xiàn)學(xué)生信息添加、刪除,修改、查詢功能。根據(jù)需求,創(chuàng)建一個學(xué)生類和學(xué)生管理類,用容器存儲學(xué)生信息,這里用到ArrayList<E>(ArrayList類是一個可動態(tài)修改的數(shù)組,使用之前需要導(dǎo)包import java.util.ArrayList;)
創(chuàng)建集合對象
ArrayList<E>?名 = new ArrayList<E>( );? ?{<E>是一種特殊的數(shù)據(jù)類型,泛型}
例:ArrayList<String>? students = new ArrayList<String>( );
其中有如下方法:
add(element)將指定元素追加到集合末尾
add (int index,element) 在指定索引下添加元素
remove(element)刪除指定元素
remove (int index)刪除指定索引下的元素
set(int index,element)修改指定索引下的元素
get(int index)返回索引下的元素
size() 返回元素個數(shù)?
一、建立學(xué)生類
學(xué)生類中應(yīng)包含有學(xué)生姓名、年齡、學(xué)號、籍貫。
public class Student {private String name;private String age;private String id;private String adress;public Student() {}public Student(String name, String age, String id, String adress) {this.name = name;this.age = age;this.id = id;this.adress = adress;}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 getId() {return id;}public void setId(String id) {this.id = id;}public String getAdress() {return adress;}public void setAdress(String adress) {this.adress = adress;} }二、創(chuàng)建學(xué)生管理類
設(shè)計初始界面
六種功能選擇用switch語句實現(xiàn)?
用方法實現(xiàn)增、刪、改、查功能
添加學(xué)生信息的方法
public static void? adds(ArrayList<Student> student) {
Scanner sc = new Scanner(System.in);System.out.println("請輸入學(xué)生姓名:");String name = sc.nextLine();System.out.println("請輸入學(xué)生年齡:");String age =sc.nextLine();System.out.println("請輸入學(xué)生學(xué)號:");String id =sc.nextLine();System.out.println("請輸入學(xué)生地址:");String adress =sc.nextLine();Student students = new Student();//創(chuàng)建學(xué)生對象students.setName(name); students.setAge(age);students.setId(id);students.setAdress(adress); //將輸入的值賦給學(xué)生對象的成員變量student.add(students); //講學(xué)生對象添加到集合中System.out.println("添加學(xué)生信息成功"); }刪除學(xué)生信息的方法
public static void removes(ArrayList<Student> student){Scanner sc = new Scanner(System.in);System.out.println("請輸入要刪除學(xué)生的學(xué)號:");String id = sc.nextLine();ugai用for循環(huán)遍歷student集合找到與輸入學(xué)號相同的學(xué)號從而找到對應(yīng)學(xué)生類對象再用remove()方法刪除 */for(int i = 0;i < student.size();i++){Student s = student.get(i); //epuals()方法將字符串與指定對象進行比較,返回是否相同;if(s.getId().equals(id)){System.out.println("刪除后無法恢復(fù),是否要繼續(xù)刪除");System.out.print("1:是\n2:否\n");int a = sc.nextInt();if(a==1) {student.remove(i);break;}else{break;}}} }修改學(xué)生信息方法
public static void setOne(ArrayList<Student> student){Scanner sc = new Scanner(System.in);System.out.println("請輸入所要修改學(xué)生的學(xué)號");String sid = sc.nextLine();System.out.println("請輸入新的學(xué)生姓名:");String name = sc.nextLine();System.out.println("請輸入新的學(xué)生年齡:");String age =sc.nextLine();System.out.println("請輸入新的學(xué)生學(xué)號:");String id =sc.nextLine();System.out.println("請輸入新的學(xué)生地址:");String adress =sc.nextLine();Student s = new Student();s.setName(name);s.setAge(age);s.setId(id);s.setAdress(adress);//輸入要修改的信息并把它賦值給新的學(xué)生類對象for(int i = 0;i < student.size();i++){Student a = student.get(i);if(a.getId().equals(sid)){student.set(i,s); /*通過equals()方法找到對應(yīng)學(xué)生類對象再用set(int index,element)方法將舊的學(xué)生類對象修改為新的*/break;}System.out.println("修改學(xué)生信息成功");}}查詢學(xué)生信息方法
public static void printAll(ArrayList<Student> student){Scanner sc = new Scanner(System.in); //先做判斷,集合中是否有信息if(student.size()==0){System.out.println("無學(xué)生信息,請先添加學(xué)生信息");System.out.println("請選擇是否添加學(xué)生信息:");System.out.println("1:添加學(xué)生信息\n2:退出\n");int n = sc.nextInt();switch (n){case 1:adds(student);break;case 2:System.exit(0);break;default:System.out.println("你的選擇有誤,請選擇已存在的業(yè)務(wù)");}}else{//遍歷集合System.out.println("學(xué)生姓名\t年齡\t學(xué)號\t地址\t");for(int i = 0;i < student.size();i++) {Student s = student.get(i);System.out.println(s.getName() + "\t" + s.getAge() + "歲\t" + s.getId() + "\t" + s.getAdress() + "\t");}} }學(xué)生管理類代碼如下:
import java.util.ArrayList; import java.util.Scanner; public class StudentManage {public static void main(String[] args) {ArrayList<Student> student= new ArrayList<Student>();Scanner sc = new Scanner(System.in);while(true) {System.out.println("--------歡迎使用學(xué)生管理系統(tǒng)--------");System.out.println("| 1:增加學(xué)生信息 |");System.out.println("| 2:刪除學(xué)生信息 |");System.out.println("| 3:修改學(xué)生信息 |");System.out.println("| 4:查找學(xué)生信息 |");System.out.println("| 5:查看所有學(xué)生信息 |");System.out.println("| 6:退出管理系統(tǒng) |");System.out.println("--------------------------------");System.out.println("請選擇要進行的業(yè)務(wù):");int n = sc.nextInt();switch (n) {case 1:System.out.println("增加學(xué)生信息");adds(student);break;case 2:removes(student);break;case 3:setOne(student);break;case 4:printOne(student);break;case 5:printAll(student);break;case 6:System.exit(0);break;default:System.out.println("你的選擇有誤,請選擇現(xiàn)有的業(yè)務(wù)");}}}//增加學(xué)生信息方法public static void adds(ArrayList<Student> student){Scanner sc = new Scanner(System.in);System.out.println("請輸入學(xué)生姓名:");String name = sc.nextLine();System.out.println("請輸入學(xué)生年齡:");String age =sc.nextLine();System.out.println("請輸入學(xué)生學(xué)號:");String id =sc.nextLine();System.out.println("請輸入學(xué)生地址:");String adress =sc.nextLine();Student students = new Student();students.setName(name);students.setAge(age);students.setId(id);students.setAdress(adress);student.add(students);System.out.println("添加學(xué)生信息成功");}//刪除學(xué)生信息public static void removes(ArrayList<Student> student){Scanner sc = new Scanner(System.in);System.out.println("請輸入要刪除學(xué)生的學(xué)號:");String id = sc.nextLine();for(int i = 0;i < student.size();i++){Student s = student.get(i);if(s.getId().equals(id)){System.out.println("刪除后無法恢復(fù),時候要繼續(xù)刪除");System.out.print("1:是\n2:否\n");int a = sc.nextInt();if(a==1) {student.remove(i);break;}else{break;}}}}//修改學(xué)生信息public static void setOne(ArrayList<Student> student){Scanner sc = new Scanner(System.in);System.out.println("請輸入所要修改學(xué)生的學(xué)號");String sid = sc.nextLine();System.out.println("請輸入新的學(xué)生姓名:");String name = sc.nextLine();System.out.println("請輸入新的學(xué)生年齡:");String age =sc.nextLine();System.out.println("請輸入新的學(xué)生學(xué)號:");String id =sc.nextLine();System.out.println("請輸入新的學(xué)生地址:");String adress =sc.nextLine();Student s = new Student();s.setName(name);s.setAge(age);s.setId(id);s.setAdress(adress);for(int i = 0;i < student.size();i++){Student a = student.get(i);if(a.getId().equals(sid)){student.set(i,s);break;}System.out.println("修改學(xué)生信息成功");}}//查找學(xué)生信息public static void printOne(ArrayList<Student> student){Scanner sc = new Scanner(System.in);System.out.println("請選擇所要查找學(xué)生的學(xué)號");String id = sc.nextLine();for(int i = 0; i < student.size();i++){Student s = student.get(i);if(s.getId().equals(id)){System.out.println("學(xué)生姓名\t年齡\t學(xué)號\t地址\t");System.out.println(s.getName()+"\t"+s.getAge()+"歲\t"+s.getId()+"\t"+s.getAdress()+"\t");}}}//查找所有學(xué)生信息public static void printAll(ArrayList<Student> student){Scanner sc = new Scanner(System.in);if(student.size()==0){System.out.println("無學(xué)生信息,請先添加學(xué)生信息");System.out.println("請選擇是否添加學(xué)生信息:");System.out.println("1:添加學(xué)生信息\n2:退出\n");int n = sc.nextInt();switch (n){case 1:adds(student);break;case 2:System.exit(0);break;default:System.out.println("你的選擇有誤,請選擇已存在的業(yè)務(wù)");}}else{System.out.println("學(xué)生姓名\t年齡\t學(xué)號\t地址\t");for(int i = 0;i < student.size();i++) {Student s = student.get(i);System.out.println(s.getName() + "\t" + s.getAge() + "歲\t" + s.getId() + "\t" + s.getAdress() + "\t");}}} }總結(jié)
以上是生活随笔為你收集整理的java学生管理系统(简单版)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于深度学习,这些知识点你需要了解一下
- 下一篇: java学生管理系统登录注册_《Java