用java寫的話,可以用List來(lái)實(shí)現(xiàn)學(xué)生管理系統(tǒng):\x0d\x0a首先,管理系統(tǒng)是針對(duì)學(xué)生對(duì)象的,所以我們先把學(xué)生對(duì)象就寫出來(lái):\x0d\x0apackage bean;\x0d\x0apublic class Student {\x0d\x0a String name;\x0d\x0a String studentId;\x0d\x0a String sex;\x0d\x0a int grade;\x0d\x0a public Student(String name,String studentId,String sex,int grade){\x0d\x0a this.name= name;\x0d\x0a this.studentId= studentId;\x0d\x0a this.sex = sex;\x0d\x0a this.grade = grade; \x0d\x0a }\x0d\x0a public int getGrade(){\x0d\x0a return grade;\x0d\x0a }\x0d\x0a public String getName(){\x0d\x0a return name;\x0d\x0a }\x0d\x0a public String getSex(){\x0d\x0a return sex;\x0d\x0a }\x0d\x0a public void setGrade(int g){\x0d\x0a this.grade = g;\x0d\x0a }\x0d\x0a public String getStudentId(){\x0d\x0a return studentId;\x0d\x0a }\x0d\x0a}\x0d\x0a這里面定義了一些得到當(dāng)前學(xué)生對(duì)象數(shù)據(jù)的一些get方法,和成績(jī)修改的set方法,代碼很簡(jiǎn)單,就不做詳細(xì)的解答。\x0d\x0a就下來(lái)就是我們的正文了。\x0d\x0a雖然我們暫時(shí)不用swing來(lái)做界面,但是總得要看的過(guò)去吧,所以,先做了一個(gè)比較簡(jiǎn)單的界面:\x0d\x0a System.out.println("***************");\x0d\x0a System.out.println("*歡迎來(lái)到學(xué)生管理系統(tǒng) *");\x0d\x0a System.out.println("*1:增加學(xué)生 *");\x0d\x0a System.out.println("*2:刪除學(xué)生 *");\x0d\x0a System.out.println("*3:修改成績(jī) *");\x0d\x0a System.out.println("*4:查詢成績(jī) *");\x0d\x0a System.out.println("***************");\x0d\x0a System.out.println("您想選擇的操作是:");\x0d\x0a這里可以看到,我們的是用一個(gè)1234來(lái)選擇項(xiàng)目,說(shuō)以不得不講一下Java如何獲取到鍵盤所輸入的數(shù)據(jù)---------Scanner ,要使用這個(gè),首先需要import進(jìn)來(lái)一個(gè)包:\x0d\x0a例如這里:\x0d\x0aimport java.util.*;\x0d\x0a之后的兩行代碼搞定輸入:\x0d\x0aScanner sc = new Scanner(System.in);\x0d\x0a int choice = sc.nextInt();\x0d\x0a接下來(lái)就是各個(gè)功能的實(shí)現(xiàn):\x0d\x0a\x0d\x0apackage test;\x0d\x0aimport java.util.*;\x0d\x0aimport bean.Student;\x0d\x0apublic class Manager {\x0d\x0a static List StudentList = new LinkedList();\x0d\x0a public static void main(String[] agrs){\x0d\x0a select(StudentList); \x0d\x0a }\x0d\x0a private static void select(List StudentList ){\x0d\x0a System.out.println("***************");\x0d\x0a System.out.println("*歡迎來(lái)到學(xué)生管理系統(tǒng) *");\x0d\x0a System.out.println("*1:增加學(xué)生 *");\x0d\x0a System.out.println("*2:刪除學(xué)生 *");\x0d\x0a System.out.println("*3:修改成績(jī) *");\x0d\x0a System.out.println("*4:查詢成績(jī) *");\x0d\x0a System.out.println("***************");\x0d\x0a System.out.println("您想選擇的操作是:");\x0d\x0a Scanner sc = new Scanner(System.in);\x0d\x0a int choice = sc.nextInt(); \x0d\x0a switch(choice){\x0d\x0a //增加學(xué)生\x0d\x0a case 1:\x0d\x0a System.out.print("請(qǐng)輸入學(xué)生的姓名:");\x0d\x0a Scanner Sname = new Scanner(System.in);\x0d\x0a String name = Sname.nextLine();\x0d\x0a System.out.print("請(qǐng)輸入學(xué)生的性別:");\x0d\x0a Scanner Ssex = new Scanner(System.in);\x0d\x0a String sex = Ssex.nextLine();\x0d\x0a System.out.print("請(qǐng)輸入學(xué)生的學(xué)號(hào):");\x0d\x0a Scanner SId = new Scanner(System.in);\x0d\x0a String studentId = SId.nextLine();\x0d\x0a System.out.print("請(qǐng)輸入學(xué)生的成績(jī):");\x0d\x0a Scanner Sgrade = new Scanner(System.in);\x0d\x0a int grade = Sgrade.nextInt();\x0d\x0a StudentList.add(new Student(name,studentId,sex,grade));\x0d\x0a System.out.println("添加成功!?。。?!");\x0d\x0a select(StudentList);\x0d\x0a break;\x0d\x0a //刪除學(xué)生成績(jī)\x0d\x0a case 2:\x0d\x0a System.out.print("請(qǐng)告訴我需要?jiǎng)h除學(xué)生的學(xué)號(hào):");\x0d\x0a Scanner Sid = new Scanner(System.in);\x0d\x0a String SstudentId = Sid.nextLine();\x0d\x0a boolean isfindDelete = false;\x0d\x0a for (int i = 0; i
創(chuàng)新互聯(lián)建站2013年開(kāi)創(chuàng)至今,先為大理州等服務(wù)建站,大理州等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為大理州企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
回答于?2022-11-16
import?java.util.*;
import?java.io.*;
class?StuMgr{
public?static?class?Student{
public?int?id;
public?String?name;
public?int?age;
public?Student(int?id?,String?name,int?age){
this.id?=?id;
this.name?=?name;
this.age?=?age;
}
@Override
public?String?toString(){
return?id?+?","?+?name?+?","?+?age;
}
}
public?ListStudent?stuList?=?new?LinkedList();
public?void?add(){
Scanner?sc?=?new?Scanner(System.in);?
System.out.println("請(qǐng)輸入學(xué)生學(xué)號(hào):");
String?id?=?sc.nextLine();
int?intId?=?0;
try{
intId?=?Integer.parseInt(id);
}catch(NumberFormatException?ex){
System.out.println("學(xué)號(hào)輸入有誤,請(qǐng)輸入數(shù)字!");
return;
}
if?(find(intId)?!=?null){
System.out.println("該學(xué)號(hào)已經(jīng)存在!");
return?;
}
System.out.println("請(qǐng)輸入學(xué)生姓名:");
String?name?=?sc.nextLine();
System.out.println("請(qǐng)輸入學(xué)生年齡:");
String?age?=?sc.nextLine();
int?intAge?=?0;
try{
intAge?=?Integer.parseInt(age);
}catch(NumberFormatException?ex){
System.out.println("年齡輸入有誤,請(qǐng)輸入數(shù)字!");
return;
}
Student?stu?=?new?Student(intId,name,intAge);
stuList.add(stu);
store();
System.out.println("-----------------------");
System.out.println("學(xué)生信息已增加");
System.out.println(stu);
System.out.println("-----------------------");
}
public?void?del(){
Scanner?sc?=?new?Scanner(System.in);?
System.out.println("請(qǐng)輸入學(xué)生學(xué)號(hào):");
String?id?=?sc.nextLine();
int?intId?=?0;
try{
intId?=?Integer.parseInt(id);
}catch(NumberFormatException?ex){
System.out.println("學(xué)號(hào)輸入有誤,請(qǐng)輸入數(shù)字!");
return;
}
Student?stu?=?find(intId);
if?(?stu?==?null){
System.out.println("該學(xué)號(hào)不存在!");
return?;
}
stuList.remove(stu);
store();
System.out.println("-----------------------");
System.out.println("學(xué)生信息已刪除");
System.out.println(stu);
System.out.println("-----------------------");
}
public?void?find(){
Scanner?sc?=?new?Scanner(System.in);?
System.out.println("請(qǐng)輸入學(xué)生學(xué)號(hào):");
String?id?=?sc.nextLine();
int?intId?=?0;
try{
intId?=?Integer.parseInt(id);
}catch(NumberFormatException?ex){
System.out.println("學(xué)號(hào)輸入有誤,請(qǐng)輸入數(shù)字!");
return;
}
Student?stu?=?find(intId);
if?(?stu?==?null){
System.out.println("該學(xué)號(hào)不存在!");
return?;
}
System.out.println("-----------------------");
System.out.println("查找學(xué)生信息如下");
System.out.println(stu);
System.out.println("-----------------------");
}
public?Student?find(int?id){
for(Student?stu?:?stuList){
if(stu.id?==?id){
return?stu;
}
}
return?null;
}
public?void?modify(){
store();
}
public?void?foreach(){
System.out.println("-----------------------");
for(Student?stu?:?stuList){
System.out.println(stu);
}
System.out.println("-----------------------");
}
public?void?store(){
Iterator?iterator?=?stuList.iterator();
File?file?=?new?File("stuList.txt");
FileWriter?fw?=?null;
BufferedWriter?writer?=?null;
try?{
fw?=?new?FileWriter(file);
writer?=?new?BufferedWriter(fw);
while(iterator.hasNext()){
writer.write(iterator.next().toString());
writer.newLine();//換行
}
writer.flush();
}?catch?(FileNotFoundException?e)?{
e.printStackTrace();
}catch?(IOException?e)?{
e.printStackTrace();
}finally{
try?{
writer.close();
fw.close();
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
public?static?void?main(String[]?args){
StuMgr?mgr?=?new?StuMgr();
while(true){
System.out.println("請(qǐng)選擇您要進(jìn)行的操作:");
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:退出");
System.out.println("-----------------------");
Scanner?sc?=?new?Scanner(System.in);?
String?op?=?sc.nextLine();
if("6".equals(op)){
return;
}
if("1".equals(op)){
mgr.add();
}
if("2".equals(op)){
mgr.del();
}
if("3".equals(op)){
mgr.find();
}
if("4".equals(op)){
mgr.modify();
}
if("5".equals(op)){
mgr.foreach();
}
}
}
}
時(shí)間倉(cāng)促,還有一個(gè)modify方法沒(méi)實(shí)現(xiàn),留給你自己練手。
import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.Comparator;
import?java.util.List;
public?class?Sort?{
public?static?void?main(String[]?args)?{
Student?p1?=?new?Student(1001,?"小明",?20);
Student?p2?=?new?Student(1002,?"小紅",?21);
Student?p3?=?new?Student(1003,?"小黑",?19);
ListStudent?list?=?new?ArrayListStudent();
list.add(p1);
list.add(p2);
list.add(p3);
Collections.sort(list,?new?ComparatorStudent()?{
/*
?*?int?compare(Student?o1,?Student?o2)?返回一個(gè)基本類型的整型,?返回負(fù)數(shù)表示:o1?小于o2,
?*?返回0?表示:o1和o2相等,?返回正數(shù)表示:o1大于o2。
?*/
public?int?compare(Student?o1,?Student?o2)?{
//?按照學(xué)生的學(xué)號(hào)進(jìn)行升序排列
if?(o1.getId()??o2.getId())?{
return?1;
}
if?(o1.getId()?==?o2.getId())?{
return?0;
}
return?-1;
}
});
write(list);
System.out.println("---------------------");
Collections.sort(list,?new?ComparatorStudent()?{
/*
?*?int?compare(Student?o1,?Student?o2)?返回一個(gè)基本類型的整型,?返回負(fù)數(shù)表示:o1?小于o2,
?*?返回0?表示:o1和o2相等,?返回正數(shù)表示:o1大于o2。
?*/
public?int?compare(Student?o1,?Student?o2)?{
//?按照學(xué)生的年齡進(jìn)行升序排列
if?(o1.getAge()??o2.getAge())?{
return?1;
}
if?(o1.getAge()?==?o2.getAge())?{
return?0;
}
return?-1;
}
});
write(list);
}
public?static?void?write(ListStudent?list)?{
for?(Student?s?:?list)?{
System.out.println(s.getId()?+?"\t"?+?s.getName()?+?"\t"
+?s.getAge());
}
}
}
public?class?Student?{
private?int?id?;
private?String?name;
private?int?age;
//構(gòu)造方法
public?Student(int?id,String?name,int?age){
this.id?=?id;
this.name?=?name;
this.age?=?age;
}
public?int?getId()?{
return?id;
}
public?void?setId(int?id)?{
this.id?=?id;
}
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;
}
}