急求一个作业,java代码
- 提问者网友:浮克旳回音
- 2021-12-22 20:05
java代码哦
- 五星知识达人网友:渊鱼
- 2021-12-22 21:14
- 1楼网友:西风乍起
- 2021-12-22 21:56
- 2楼网友:深街酒徒
- 2021-12-22 21:41
刚写的:
学生类:
public class student { private int id; private string name; private string xuehao; private string chengji; 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 string getxuehao() { return xuehao; } public void setxuehao(string xuehao) { this.xuehao = xuehao; } public string getchengji() { return chengji; } public void setchengji(string chengji) { this.chengji = chengji; } }
测试类:
import java.util.arraylist; import java.util.list; import java.util.scanner;
public class stutest { private list<student> list = new arraylist<student>(); public boolean isnull(string string){ if (null == string || "" == string || 0 == string.length()) { return true; } else { return false; } } public void addstudent(scanner sc){ system.out.println("——————增加学生信息——————"); while(true){ system.out.println("姓名:"); string name = sc.next(); system.out.println("学号:"); string xuehao = sc.next(); system.out.println("成绩:"); string chengji = sc.next(); if (!isnull(name) && !isnull(xuehao)) { student student = new student(); student.setid(list.size() + 1); student.setname(name); student.setxuehao(xuehao); student.setchengji(chengji); list.add(student); system.out.println("添加成功"); system.out.println("0、exit 1、继续"); int n = sc.nextint(); if (n == 0) { system.out.println("—————增加学生信息结束—————"); break; } } else { system.out.println("姓名或学号不能为空"); } } } public void delstudent(scanner sc){ system.out.println("——————删除学生信息——————"); while(true){ system.out.println("你将根据什么删除?:1、名称 2、学号 3、成绩"); string condition= ""; boolean bool = false; int n = sc.nextint(); if (n == 1) { system.out.println("请输入要删除的姓名:"); string name = sc.next(); for (int i = 0; i < list.size(); i++) { if (list.get(i).getname().equals(name)) { list.remove(i); bool = true; } } } else if (n == 2) { system.out.println("请输入要删除的学号:"); string xuehao = sc.next(); for (int i = 0; i < list.size(); i++) { if (list.get(i).getxuehao().equals(xuehao)) { list.remove(i); bool = true; } } } else if (n == 3) { system.out.println("请输入要删除的成绩:"); string chengji = sc.next(); for (int i = 0; i < list.size(); i++) { if (list.get(i).getchengji().equals(chengji)) { list.remove(i); bool = true; } } } if (bool) { system.out.println("删除成功"); } else { system.out.println("删除失败或相关信息不存在"); } system.out.println("0、exit 1、继续"); int m = sc.nextint(); if (m == 0) { system.out.println("—————删除学生信息结束—————"); break; } } } public void updatestudent(scanner sc){ system.out.println("——————修改学生信息——————"); while(true){ system.out.println("请输入你要修改的学生的id:"); int id = sc.nextint(); system.out.println("新姓名:"); string name = sc.next(); system.out.println("新学号:"); string xuehao = sc.next(); system.out.println("新成绩:"); string chengji = sc.next(); student student = list.get(id); if (!isnull(name)) { student.setname(name); } if (!isnull(xuehao)) { student.setxuehao(xuehao); } if (!isnull(chengji)) { student.setchengji(chengji); } system.out.println("修改成功"); system.out.println("0、exit 1、继续"); int n = sc.nextint(); if (n == 0) { system.out.println("—————修改学生信息结束—————"); break; } } } public void querystudent(scanner sc){ system.out.println("——————查询学生信息——————"); while(true){ system.out.println("选择查询方式:1、单人查询 2、多人查询"); int n = sc.nextint(); if (n == 1) { system.out.println("请输入要查询的学生id:"); int id = sc.nextint(); student student = null; for (int i = 0; i < list.size(); i++) { if (list.get(i).getid() == id) { student = list.get(i); break; } } if (student != null) { system.out.println("——————信息——————"); system.out.println("id\t姓名\t学号\t成绩"); system.out.println(student.getid() + "\t" + student.getname() + "\t" + student.getxuehao() + "\t" + student.getchengji()); system.out.println("——————结束——————"); } else { system.out.println("没有找到相关信息"); } system.out.println("0、exit 1、继续"); int m = sc.nextint(); if (m == 0) { system.out.println("—————查询学生信息结束—————"); break; } } else if (n == 2) { system.out.println("请选择要查询的学生条件(模糊查询):1、名字 2、学号 3、成绩"); int m = sc.nextint(); list<student> liststu = new arraylist<student>(); string condition = ""; if (m == 1) { system.out.println("请输入姓名的一个或多个字:"); string name = sc.next(); for (int i = 0; i < list.size(); i++) { if (islike(list.get(i).getname(), name)) { liststu.add(list.get(i)); } } } else if (m == 2) { system.out.println("请输入学号的部分或全部(从左往右查找):"); string xuehao = sc.next(); for (int i = 0; i < list.size(); i++) { if (islike(list.get(i).getxuehao(), xuehao)) { liststu.add(list.get(i)); } } } else if (m == 3) { system.out.println("请输入成绩(精确):"); string chengji = sc.next(); for (int i = 0; i < list.size(); i++) { if (list.get(i).getchengji().equals(chengji)) { liststu.add(list.get(i)); } } } if (liststu.size() != 0) { system.out.println("——————信息——————"); system.out.println("id\t姓名\t学号\t成绩"); for (student student : list) { system.out.println(student.getid() + "\t" + student.getname() + "\t" + student.getxuehao() + "\t" + student.getchengji()); } system.out.println("——————结束——————"); } else { system.out.println("没有找到相关信息"); } system.out.println("0、exit 1、继续"); int l = sc.nextint(); if (l == 0) { system.out.println("—————查询学生信息结束—————"); break; } } } } public boolean islike(string parent, string child){ if (!isnull(parent) && !isnull(child)) { if (parent.substring(0, child.length()).equals(child)) { return true; } else { return false; } } else { return false; } } public static void main(string[] args) { stutest test = new stutest(); scanner sc = new scanner(system.in); system.out.println("————欢迎使用学生信息管理系统————"); while(true){ system.out.println("请选择你要进行的操作:1、添加信息 2、删除信息 3、修改信息 4、查询 0、退出"); int n = sc.nextint(); if (n == 1) { test.addstudent(sc); } else if (n == 2) { test.delstudent(sc); } else if (n == 3) { test.updatestudent(sc); } else if (n == 4) { test.querystudent(sc); } else if (n == 0) { system.out.println("————谢谢使用————"); break; } } } }