好友
阅读权限10
听众
最后登录1970-1-1
|
分享议一下代码:
首先是自定义学生类,代码如下:
package com.itheima;
public class Student {
private String sid;
private String name;
private String age;
private String address;
public Student() {
super();
}
public Student(String sid, String name, String age, String address) {
super();
this.sid = sid;
this.name = name;
this.age = age;
this.address = address;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
学管理系统主代码:
package com.itheima;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManangerTest {
public static void main(String[] args) {
ArrayList<Student> array = new ArrayList<Student>();
while (true) {
System.out.println("--------欢迎来到学生管理系统,请选择:--------");
System.out.println("1 查看所有学生");
System.out.println("2 添加学生");
System.out.println("3 删除学生");
System.out.println("4 修改学生");
System.out.println("5 退出");
System.out.println("请输入你的选择:");
Scanner sc = new Scanner(System.in);
String choiceString = sc.nextLine();
switch (choiceString) {
case "1":
findAllStudent(array);
break;
case "2":
addStudent(array);
break;
case "3":
deleteStudent(array);
break;
case "4":
updateStudent(array);
break;
case "5":
default:
System.out.println("谢谢你的使用 ");
System.exit(0);
break;
}
}
}
private static void updateStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要修改学生的学号:");
String sid = sc.nextLine();
int index = -1;
for (int x = 0; x < array.size(); x++) {
Student s = array.get(x);
if (s.getSid().equals(sid)) {
index = x;
break;
}
}
if (index != -1) {
System.out.println("请输入新姓名:");
String name = sc.nextLine();
System.out.println("请输入新年龄:");
String age = sc.nextLine();
System.out.println("请输入新地址:");
String address = sc.nextLine();
Student s = new Student();
s.setSid(sid);
s.setName(name);
s.setAge(age);
s.setAddress(address);
array.set(index, s);
System.out.println("修改学生成功");
} else {
System.out.println("不好意思,你要修改的学号对应的学生信息不存在,请回去重新选择你的操作");
return;
}
}
private static void deleteStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要删除学生的学号:");
String sid = sc.nextLine();
int index = -1;
for (int x = 0; x < array.size(); x++) {
Student s = array.get(x);
if (s.getSid().equals(sid)) {
index = x;
break;
}
}
if (index != -1) {
array.remove(index);
System.out.println("删除学生成功");
} else {
System.out.println("不好意思,你要删除的学号对应的学生信息不存在,请回去重新选择你的操作");
return;
}
}
private static void addStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
String sid;
while (true) {
System.out.println("请输入学号:");
sid = sc.nextLine();
boolean flag = false;
for (int x = 0; x < array.size(); x++) {
Student s = array.get(x);
if (s.getSid().equals(sid)) {
flag = true;
}
}
if (flag == true) {
System.out.println("你输入的学号已经被占用,请重新输入");
} else {
break;
}
}
System.out.println("请输入姓名:");
String name = sc.nextLine();
System.out.println("请输入年龄:");
String age = sc.nextLine();
System.out.println("请输入地址:");
String address = sc.nextLine();
Student s = new Student();
s.setSid(sid);
s.setName(name);
s.setAge(age);
s.setAddress(address);
array.add(s);
System.out.println("添加学生成功");
}
private static void findAllStudent(ArrayList<Student> array) {
if (array.size() == 0) {
System.out.println("不好意思,目前没有学生信息可供查看,请回去重新选择你的操作");
return;
}
System.out.println("学号\t\t姓名\t年龄\t居住地");
for (int x = 0; x < array.size(); x++) {
Student s = array.get(x);
System.out.println(s.getSid() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress());
}
} |
免费评分
-
查看全部评分
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|