Java實現(xiàn)簡單學生管理系統(tǒng)
本文實例為大家分享了Java實現(xiàn)簡單學生管理系統(tǒng)的具體代碼,供大家參考,具體內容如下
名為StudentManageTest的Java測試類
import java.util.Scanner; ? public class StudentManageTest { ? ? public static void main(String[] args) { ? ? ? ? StudentManage stu = new StudentManage(); ? ? ? ? Scanner sc = new Scanner(System.in); ? ? ? ? ? while (true){ ? ? ? ? System.out.println("-------------------歡迎來到學生管理系統(tǒng)------------------------"); ? ? ? ? System.out.println("------ ? ? ? ? ? ? ?1. 查看所有學生 ? ? ? ? ? ? ? ? ? ? ?-----"); ? ? ? ? System.out.println("------ ? ? ? ? ? ? ?2. 添加學生 ? ? ? ? ? ? ? ? ? ? ? ? ?-----"); ? ? ? ? System.out.println("------ ? ? ? ? ? ? ?3. 刪除學生 ? ? ? ? ? ? ? ? ? ? ? ? ?-----"); ? ? ? ? System.out.println("------ ? ? ? ? ? ? ?4. 修改學生 ? ? ? ? ? ? ? ? ? ? ? ? ?-----"); ? ? ? ? System.out.println("------ ? ? ? ? ? ? ?5. 查詢學生 ? ? ? ? ? ? ? ? ? ? ? ? ?-----"); ? ? ? ? System.out.println("------ ? ? ? ? ? ? ?6. 退出 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-----"); ? ? ? ? System.out.println("--------------------------------------------------------------"); ? ? ? ? System.out.print("請輸入您的選擇:"); ? ? ? ? int num = sc.nextInt(); ? ? ? ? if(num==1){ ? ? ? ? ? ? stu.show(); ? ? ? ? } ? ? ? ? if(num==2) { ? ? ? ? ? ? System.out.println("姓名:"); ? ? ? ? ? ? String name = sc.next(); ? ? ? ? ? ? System.out.println("學號:"); ? ? ? ? ? ? int id = sc.nextInt(); ? ? ? ? ? ? System.out.println("成績:"); ? ? ? ? ? ? int score = sc.nextInt(); ? ? ? ? ? ? Student s = new Student(name, id, score); ? ? ? ? ? ? stu.add(s); ? ? ? ? ? ? System.out.println("添加學生成功?。?); ? ? ? ? } ? ? ? ? if(num==3){ ? ? ? ? ? ? System.out.println("請輸入需要刪除第幾個學生信息:"); ? ? ? ? ? ? int id = sc.nextInt(); ? ? ? ? ? ? stu.delete(id); ? ? ? ? ? ? System.out.println("刪除成功??!"); ? ? ? ? } ? ? ? ? if(num==4){ ? ? ? ? ? ? System.out.println("請輸入需要修改第幾個學生信息:"); ? ? ? ? ? ? int n = sc.nextInt(); ? ? ? ? ? ? System.out.println("姓名:"); ? ? ? ? ? ? String name = sc.next(); ? ? ? ? ? ? System.out.println("學號:"); ? ? ? ? ? ? int id = sc.nextInt(); ? ? ? ? ? ? System.out.println("成績:"); ? ? ? ? ? ? int score = sc.nextInt(); ? ? ? ? ? ? Student ch = new Student(name, id, score); ? ? ? ? ? ? stu.change(n,ch); ? ? ? ? ? ? System.out.println("修改成功??!"); ? ? ? ? } ? ? ? ? if(num==5){ ? ? ? ? ? ? System.out.println("請輸入你需要查詢第幾個學生信息:"); ? ? ? ? ? ? int n = sc.nextInt(); ? ? ? ? ? ? stu.check(n); ? ? ? ? } ? ? ? ? if(num==6){ ? ? ? ? ? ? System.out.println("下次再來??!"); ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? } ? ? } }
名為Student的成員方法定義類
public class Student { ? ? private String name; ? ? private int id; ? ? private int score; ? ? ? public Student() { ? ? } ? ? ? public Student(String name, int id, int score) { ? ? ? ? this.name = name; ? ? ? ? this.id = id; ? ? ? ? this.score = score; ? ? } ? ? ? public String show() { ? ? ? ? return name +"\t\t" + id + "\t\t\t" + score; ? ? } }
名為StudentManage的方法類
public class StudentManage{ ? ? //初始三個學生對象,定義到一個叫ss的數(shù)組對象中 ? ? Student[] ss = new Student[]{new Student("張三",1,70), ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new Student("李四",2,80), ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new Student("王五",3,85)}; ? ? //添加操作 ? ? public void add(Student s){ ?//傳入一個自定義的對象數(shù)據(jù) ? ? ? ? Student[] newss = new Student[ss.length+1]; ? ? ? ? /*添加操作數(shù)組 ?因為數(shù)組一旦定義就無法改變長度 ? ? ? ? * 所以需要新定義比ss對象數(shù)組多一個長度的數(shù)組對象 ? ? ? ? * */ ? ? ? ? for (int i = 0; i <ss.length ; i++) { ?//該處循環(huán)的目的是把ss數(shù)組對象的數(shù)據(jù)復制一份到newss對象數(shù)組中 ? ? ? ? ? ? newss[i] = ss[i]; ? ? ? ? } ? ? ? ? newss[newss.length-1] = s; ?//該處是因為新定義的數(shù)組長度多1,目的是把新傳入的對象數(shù)據(jù)賦值到新數(shù)組對象的最后一個 ? ? ? ? ss=newss; ?//將newss堆中的數(shù)據(jù)地址賦給ss,即ss指向堆中存有新數(shù)據(jù)的內存地址 ? ? ? ? //當執(zhí)行show方法時,遍歷輸出的是堆中新的地址的新的數(shù)據(jù) ? ? } ? ? public void show(){ ?//循環(huán)遍歷輸出ss中的數(shù)據(jù) ? ? ? ? System.out.println("姓名:\t\t學號:\t\t成績:"); ? ? ? ? for (int i = 0; i <ss.length ; i++) { ? ? ? ? ? ? System.out.println(ss[i].show()); ? ? ? ? } ? ? } ? ? public void delete(int n){ ?//刪除操作 ?刪除第n個數(shù)據(jù) ? ? ? ? //因為對象數(shù)組不能改變長度,所以得重新創(chuàng)建個比ss對象數(shù)組少一個長度的對象數(shù)組 ? ? ? ? Student[] deletess = new Student[ss.length-1]; ? ? ? ? // 因為刪除一個數(shù)據(jù),所以該數(shù)據(jù)前面數(shù)據(jù)索引和新數(shù)組對象數(shù)據(jù)的索引一樣,因此循環(huán)遍歷到n-1 ? ? ? ? for (int i = 0; i <n-1 ; i++) { ? ? ? ? ? ? //所以新數(shù)組對象n-1前面的數(shù)據(jù)一樣 ? ? ? ? ? ? deletess[i] = ss[i]; ? ? ? ? } ? ? ? ? //刪除了第n個元素,對應數(shù)組的第n-1個數(shù)據(jù),所以數(shù)組元素從(n-1)+1開始遍歷,即從n開始遍歷 ? ? ? ? for (int i = n; i <ss.length ; i++) { ? ? ? ? ? ? //所以ss的第n個元素就等于deletess的第n-1個元素 ? ? ? ? ? ? deletess[i-1] = ss[i]; ? ? ? ? } ? ? ? ? //將deletess堆中的數(shù)據(jù)地址賦給ss,即ss指向堆中存有新數(shù)據(jù)的內存地址 ? ? ? ? ss=deletess; ? ? } ? ? //修改操作,傳入?yún)?shù)含義:修改第幾個數(shù)據(jù),把它修改成什么 ? ? public void change(int n,Student ch){ ? ? ? ? ss[n-1] = ch; ?//直接將ss的第n-1個元素修改成ch ? ? } ? ? //查詢學生數(shù)據(jù),查詢第n個數(shù)據(jù) ? ? public void check(int n){ ? ? ? ? System.out.println("姓名:\t\t學號:\t\t成績:"); ? ? ? ? System.out.println(ss[n-1].show());//輸出數(shù)組對象ss的第n-1個數(shù)據(jù)的show方法 ? ? } }
測試功能:
學生管理系統(tǒng)初始化界面
顯示學生信息
新增學生信息
刪除某個學生信息
修改某個學生信息
查詢某個學生信息
退出
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java實現(xiàn)“年-月-日 上午/下午時:分:秒”的簡單代碼
當前的日期輸出的方法有很多,本文為大家介紹下在java中是如何實現(xiàn)“年-月-日 上午/下午時:分:秒”,感興趣的朋友不妨參考下2015-08-08Springboot基于websocket實現(xiàn)簡單在線聊天功能
這篇文章主要介紹了Springboot基于websocket實現(xiàn)簡單在線聊天功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06Java concurrency集合之CopyOnWriteArraySet_動力節(jié)點Java學院整理
CopyOnWriteArraySet基于CopyOnWriteArrayList實現(xiàn),其唯一的不同是在add時調用的是CopyOnWriteArrayList的addIfAbsent(若沒有則增加)方法2017-06-06springboot時間格式化的五種方法總結(解決后端傳給前端的時間顯示不一致)
這篇文章主要給大家介紹了關于springboot時間格式化的五種方法,文中介紹的方法解決了后端傳給前端的時間顯示不一致,文中通過圖文以及代碼介紹的非常詳細,需要的朋友可以參考下2024-01-01spring boot 實現(xiàn)配置多個DispatcherServlet最簡單方式
這篇文章主要介紹了spring boot 實現(xiàn)配置多個DispatcherServlet最簡單方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01