學籍管理系統(Java初級版)

來源:互聯網
上載者:User

標籤:學籍管理   java   

import java.util.Scanner;/** * 學籍管理系統 * @author Tanker * @version 4.6.0 */ public class XueJiSystem {    //Java 入口public static void main(String[] args) {    Scanner sc=new Scanner(System.in); //控制台輸入System.out.println("歡迎來到本學籍管理系統!");for(;;){System.out.println("請輸入 1.新增學生資訊  2.查詢  3.修改姓名  4.刪除  5.退出");int num=sc.nextInt(); //接收使用者輸入if(num==1){Student s=new Student();//執行個體化一個學生對象//分別給學生的屬性賦值System.out.println("請輸入學生學號:");s.setSid(sc.nextInt());System.out.println("請輸入學生姓名:");s.setName(sc.next());System.out.println("請輸入學生年齡:");s.setAge(sc.nextInt());System.out.println("請輸入學生地址:");s.setAdd(sc.next());//調用添加方法,並把學生對象傳入裡面,返回是否添加成功的標示boolean flag=StudentOperator.addStudent(s);//根據標示,判斷是否添加成功if(flag){System.out.println("添加成功!");}else{System.out.println("添加失敗!");}}else if(num==2){System.out.println("請輸入學生學號:");int sid=sc.nextInt();//根據輸入學號查詢學生資訊Student s=StudentOperator.selectStudent(sid+"");if(s==null){System.out.println("該學生資訊不存在!");}else{System.out.println(s.toString());////toString 是預設的一個方法}}else if(num==3){System.out.println("請輸入要修改姓名的學生學號:");int sid=sc.nextInt();//根據輸入的學生ID。查詢學生資訊Student s=StudentOperator.selectStudent(sid+"");//如果學生資訊存在if(s!=null){System.out.println("請設定新的姓名:");String newName=sc.next();//調用方法,並把學號、學生姓名、修改後的姓名傳入裡面,返回是否添加成功的標示boolean flag=StudentOperator.updateStudent(sid,s.getName(),newName);//判斷修改是否成功if(flag){System.out.println("修改成功!");}else{System.out.println("修改失敗!");}//如果學生資訊不存在}else{System.out.println("該學生資訊不存在!請添加");}}else if(num==4){System.out.println("請輸入要刪除的學生學號:");int sid=sc.nextInt();//根據輸入的學生ID。查詢學生資訊Student s=StudentOperator.selectStudent(sid+"");if(s!=null){//找到學生資訊之後將學號傳入刪除方法並返回是否刪除成功的標示boolean flag=StudentOperator.deleteStudent(sid);//判斷是否刪除成功if(flag){System.out.println("刪除成功!");}else{System.out.println("刪除失敗!");}}else{System.out.println("該學生資訊不存在!");}}else if(num==5){System.out.println("系統已退出!");break;}else{System.out.println("輸入有誤!請重輸");}}        }}//學生類public class Student {private int sid;// 學號private String name;// 姓名private int age;// 年齡private String add;// 家庭住址public int getSid() {return sid;}public void setSid(int sid) {this.sid = sid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getAdd() {return add;}public void setAdd(String add) {this.add = add;}//顯示查詢結果 每個類都有toString方法@Overridepublic String toString() {return "Student [sid=" + sid + ", name=" + name + ", age=" + age + ", add=" + add + "]";}}//控制類,圍繞數組展開增刪改查四大方法public class StudentOperator {static Student stu[]=new Student[10];//用於存放學生對象//查詢方法public static Student selectStudent(String sid){Student s=null;//定義一個學生類的變數s 定義為空白//遍曆數組for(int i=0;i<stu.length;i++){//當數組元素不為空白時if(stu[i]!=null){//判斷該元素的sid跟外部傳入的sid是否相等if(stu[i].getSid()==Integer.parseInt(sid)){s=stu[i];//如果相等就把該元素賦值給s}}}return s;//返回學生類s}//新增學生資訊public static boolean addStudent(Student s){//定義一個int類型的變數index,賦值為-1,用於標記數組下標int index=-1;//遍曆數組,給要添加的學生找個空位for(int i=1;i<stu.length;i++){if(stu[i]==null){index=i;//有空位就將下標賦值給index,並停止迴圈break;}}//通過index下標的值判斷添加情況if(index==-1){return false; //位置滿了添加失敗}else{stu[index]=s; //將對象學生存入數組return true;  //添加成功}}//刪除public static boolean deleteStudent(int sid){//定義一個布爾類型的變數flag 初始化為falseboolean flag=false;//遍曆數組for(int i=0;i<stu.length;i++){//當數組不為空白時if(stu[i]!=null){//根據外部傳入的sid判斷數組中是否存在if(stu[i].getSid()==sid){stu[i]=null;//存在的話將數組元素清空(賦值為空白)flag=true;//flag變更為true,同時終止迴圈break;}}}return flag; //返回布爾值flag}//修改資訊public static boolean updateStudent(int sid,String field,String newValue){//定義一個布爾類型的變數flag 初始化為falseboolean flag=false;//遍曆數組for(int i=0;i<stu.length;i++){//當數組不為空白時if(stu[i]!=null){//根據外部傳入的sid判斷數組中是否存在if(stu[i].getSid()==sid){stu[i].setName(newValue);//存在的話將數組元素的name設定為新的值flag=true;//flag變更為true,同時終止迴圈break;}}}return flag;//返回布爾值flag}}


學籍管理系統(Java初級版)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.