• --------学生管理系统--------



    import java.util.ArrayList;
    import java.util.Scanner;

    //管理系统
    //定义学生类
    //代码编写
    //查看
    //添加
    //删除
    //修改
    //结束
    public class StudentManager {
    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":
    // 结束
    // System.out.println("谢谢你的使用");
    // break;
    default:
    System.out.println("谢谢你的使用");
    System.exit(0);//JVM退出
    break;
    }
    }

    }
    //修改学生
    public static void updateStudent(ArrayList<Student>array){
    //修改学生思路,键盘录入学号,到集合中去查找看是否有学生用的是否是该学号,如果有就修改该学生
    Scanner sc=new Scanner(System.in);
    System.out.println("请输入学生的学号");
    String id =sc.nextLine();
    //定义一个索引
    int index=-1;
    //遍历集合
    for (int i = 0; i < array.size(); i++) {
    //获得一个学生对象
    Student s=array.get(i);
    //那这个学生的序号和键盘录入学生的序号做比较
    if(s.getId().equals(id)){
    index=i;
    break;
    }
    }
    if(index == -1){
    System.out.println("不好意思,你要修改的学号对应的学生不存在,请回去重新你的选择");
    }else{
    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.setId(id);
    s.setName(name);
    s.setAge(age);
    s.setAddress(address);
    }
    }
    //删除学生
    public static void deleteStudent(ArrayList<Student>array){
    //删除学生的思路,键盘录入学号,到集合中去查找看是否有学生用的是否是该学号,如果有删除该学生
    Scanner sc=new Scanner(System.in);
    System.out.println("请输入学生的学号");
    String id =sc.nextLine();
    //遍历集合
    /*for (int i = 0; i < array.size(); i++) {
    //获得一个学生对象
    Student s=array.get(i);
    //那这个学生的序号和键盘录入学生的序号做比较
    if(s.getId().equals(id)){
    array.remove(i);//根据索引删除
    break;
    }

    }
    System.out.println("删除学生成功");*/
    //必须给出学号不存在时候的提示
    //定义一个索引
    int index=-1;
    for (int i = 0; i < array.size(); i++) {
    //获得一个学生对象
    Student s=array.get(i);
    if(s.getId().equals(id)){
    index =i;
    break;
    }

    }
    if(index==-1){
    System.out.println("不好意思,你要删除的学号对应的学生不存在,请回去重新你的选择");
    }else{
    array.remove(index);
    System.out.println("删除学生成功");
    }
    }
    //添加学生
    public static void addStudent(ArrayList<Student>array){
    //键盘录入对象
    Scanner sc =new Scanner(System.in);
    //为了ID被访问到
    String id;
    //为了让代码回到这里,用循环
    while(true){
    System.out.println("请输入学生学号:");
    //String id = sc.nextLine();
    id=sc.nextLine();
    //判断学号没有被人占用
    boolean flag=false;
    //遍历集合
    for (int i = 0; i < array.size(); i++) {
    Student s = array.get(i);
    if(s.getId().equals(id)){
    flag=true;
    break;
    }
    }
    if(flag){
    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.setId(id);
    s.setName(name);
    s.setAge(age);
    s.setAddress(address);
    //把学生对象作为元素添加到集合
    array.add(s);
    System.out.println("添加学生成功");

    }
    // 查看所有学生
    public static void findAllStudent(ArrayList<Student> array){
    //首先判断集合中是否有数据,如果没有数据,就给出提示,并让方法不继续往下执行
    if(array.size()==0){
    System.out.println("不好意思,目前没有学生信息可查询返回去重新选择你的操作");
    return;
    }
    // tab键的位置
    System.out.println("学号 姓名 年龄 居住地");
    for(int x=0;x<array.size();x++){
    Student s =array.get(x);
    System.out.println(s.getId()+"---"+s.getName()+"---"+s.getAge()+"---"+s.getAddress());

    }




    }
    }

  • 相关阅读:
    MRC
    navigationcontroller手势翻页和navigationbar
    process monitor教程汇总
    Fixed: The Windows Process Activation Service service terminated with the following error: The system cannot find the file specified
    ObservableCollection 类
    玩转INotifyPropertyChanged和ObservableCollection
    在IIS中寄存服务
    WPF:为什么使用ContentPresenter.ContentSource而不是Content属性?
    正确理解WPF中的TemplatedParent
    Difference between List View and DataGrid in WPF
  • 原文地址:https://www.cnblogs.com/wangkemeng/p/6745543.html
Copyright © 2020-2023  润新知