本文主要是介绍java学生管理系统,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package itheima;
//学生类 Alt+Insert 根据自己的需要进行选择
public class Student {
//成员变量
private String sid;
private String name;
private String age;
private String address;
//构造方法
public Student(){}
public Student(String sid,String name,String age,String address){
this.name = name;
this.age = age;
this.sid = sid;
this.address = address;
}
//提供get/set方法
public void setSid(String sid){
this.sid = sid;
}
public String getSid(){
return sid;
}
//提供get/set方法
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
//提供get/set方法
public void setAge(String age) {
// if (age < 0 || age > 120) {
// System.out.println("你给的年龄有误");
// } else {
// this.age = age;
// }
this.age = age;
}
public String getAge() {
return age;
}
//提供get/set方法
public void setAddress(String address){
this.address = address;
}
public String getAddress(){
return address;
}
public void show(){
System.out.println(name+","+age);
}
}
package itheima;
import com.sun.org.apache.xalan.internal.res.XSLTErrorResources_es;
import com.sun.org.apache.xerces.internal.xs.XSIDCDefinition;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManger {
public static void main(String[] args) {
//创建集合对象
ArrayList<Student>arry = 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);
int s = sc.nextInt();
switch (s) {
case 1:addStudent(arry);
break;
case 2:deleteStudent(arry);
break;
case 3:updateStudent(arry);
break;
case 4:findSdudent(arry);
break;
case 5:
System.exit(0);//JVM推出
}
}
}
//定义添加学生方法
public static void addStudent(ArrayList<Student>arry){
Scanner sc = new Scanner(System.in);
System.out.println("请输入学号:");
String sid= sc.nextLine();
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);
arry.add(s);
System.out.println("添加学生成功");
}
//定义查看学生方法
public static void findSdudent(ArrayList<Student>arry){
if (arry.size()==0){
System.out.println("无学生信息,请添加后再查询");
}else {
System.out.println("学号\t\t\t姓名\t\t年龄\t\t居住地");// \t 就是tab键的意思
for (int i = 0; i < arry.size(); i++) {
Student s = arry.get(i);
System.out.println(s.getSid() + "\t" + s.getName() + "\t\t" + s.getAge() + "岁\t" + s.getAddress());
}
}
}
//定义删除学生方法
public static void deleteStudent(ArrayList<Student>arry){
Scanner sid = new Scanner(System.in);
System.out.println("请输入要删除学生的学号:");
for (int i = 0;i < arry.size();i++){
Student s = arry.get(i);
if (s.getSid().equals(sid)){
arry.remove(i);
break;
}
}
System.out.println("删除学生信息成功");
}
//定义修改学生方法
public static void updateStudent(ArrayList<Student>arry){
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要修改的学生学号:");
String sid = sc.nextLine();
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.setAge(age);
s.setName(name);
s.setAddress(address);
for (int i = 0;i < arry.size();i++)
{
Student s1 = arry.get(i);
if (s1.getSid().equals(sid)){
arry.set(i,s);
break;
}
}
System.out.println("修改学生成功");
}
}
这篇关于java学生管理系统的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!