You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

学生管理程序删改操作触发System.IndexOutOfRangeException求助

学生管理程序删除操作异常求助

我第一次发帖,正在开发一款支持学生删除、添加操作的程序。执行删除→添加→再次删除操作时,触发了如下异常:

Unhandled exception. System.IndexOutOfRangeException: Index is out of range

相关代码

Students.cs

namespace StudentsSelector
{
    public static class StudentsList
    {
        public static string[] AllStudents { get; private set; }
        private static int studentCount;

        // 已分配角色的属性
        public static Dictionary<string, string> AssignedRoles { get; set; }

        // 静态构造函数初始化数组
        static StudentsList()
        {
            AssignedRoles = new Dictionary<string, string>();

            AllStudents = new string[]
            {
                // 初始学生列表
                "Alex Jose Rodriguez Taveras",
                "Amaury Daniel Romero Gonzalez",
                "Ashlee Ramirez Rosario",
                "Cyd Marie Jorge Chapman",
                "Edison Yadir Rossis",
                "Edwin Oscar Perez Rodriguez",
                "Eli Samuel Suero Rodriguez",
                "Erickson David Encarnacion Encarnacion",
                "Eudy Yunior Lorenzo Ramirez",
                "Jamil Guzman Feliz",
                "Joan Manuel Arroyo Valerio",
                "Jose Miguel Canela Santos",
                "Maria Del Carmen Diaz Campanas",
                "Maria Marlene Abreu Saiz",
                "Marlon Miguel Vargas Mendez",
                "Michael Dmeshell Sanchez Heredia",
                "Odana Margarita Calderon Pache",
                "Oscar Daniel Tuletta Mercedes",
                "Rafael Antonio Urbaez Hernandez",
                "Smith Morillo Encarnacion",
                "Teudy Joshua Encarnacion Fulgencio",
                "Xander Ruddy Cruz De La Rosa",
                "Yadianna Vargas Pimentel",
                "Yafreisy Emelin Alvarez Capellan",
                "Yoelmi Alexander Alcala Valdez"
            };

            studentCount = AllStudents.Length;
        }

        // 重置学生列表(测试或初始化用)
        public static void ResetAllStudents()
        {
            AllStudents = new string[]
            {
                // 重置后的学生列表
                "Alex Jose Rodriguez Taveras",
                "Amaury Daniel Romero Gonzalez",
                "Ashlee Ramirez Rosario",
                "Cyd Marie Jorge Chapman",
                "Edison Yadir Rossis",
                "Edwin Oscar Perez Rodriguez",
                "Eli Samuel Suero Rodriguez",
                "Erickson David Encarnacion Encarnacion",
                "Eudy Yunior Lorenzo Ramirez",
                "Jamil Guzman Feliz",
                "Joan Manuel Arroyo Valerio",
                "Jose Miguel Canela Santos",
                "Maria Del Carmen Diaz Campanas",
                "Maria Marlene Abreu Saiz",
                "Marlon Miguel Vargas Mendez",
                "Michael Dmeshell Sanchez Heredia",
                "Odana Margarita Calderon Pache",
                "Oscar Daniel Tuletta Mercedes",
                "Rafael Antonio Urbaez Hernandez",
                "Smith Morillo Encarnacion",
                "Teudy Joshua Encarnacion Fulgencio",
                "Xander Ruddy Cruz De La Rosa",
                "Yadianna Vargas Pimentel",
                "Yafreisy Emelin Alvarez Capellan",
                "Yoelmi Alexander Alcala Valdez"
            };

            studentCount = AllStudents.Length;
        }

        public static void ResizeArray()
        {
            // 创建扩容后的新数组
            string[] newArray = new string[AllStudents.Length + 1];
            Array.Copy(AllStudents, newArray, AllStudents.Length);
            AllStudents = newArray;
        }

        // 删除指定索引的学生
        public static void RemoveStudentAt(int index)
        {
            if (index < 0 || index >= studentCount)
            {
                throw new IndexOutOfRangeException("Index is out of range.");
            }

            for (int i = index; i < studentCount - 1; i++)
            {
                AllStudents[i] = AllStudents[i + 1];
            }

            AllStudents[studentCount - 1] = string.Empty;
            studentCount--;
        }

        // 获取当前学生数量
        public static int GetStudentCount()
        {
            return studentCount;
        }
    }
}

DeleteStudent.cs

namespace StudentsSelector
{
    class GetRidStudents
    {
        public static void DeleteStudents()
        {
            string[] Students = StudentsList.AllStudents;

            Console.Write("请输入要删除的学生姓名: ");
            string? UserSelection = Console.ReadLine();

            if (UserSelection != null)
            {
                bool studentDeleted = false;

                for (int i = 0; i < Students.Length; i++)
                {
                    if (!string.IsNullOrEmpty(Students[i]) && Students[i].IndexOf(UserSelection, StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        StudentsList.RemoveStudentAt(i);
                        studentDeleted = true;
                        i--;
                    }
                }

                if (studentDeleted)
                {
                    Console.WriteLine("更新后的学生列表:");
                    foreach (string student in StudentsList.AllStudents)
                    {
                        if (!string.IsNullOrEmpty(student)) // 仅显示有效姓名
                        {
                            Console.WriteLine(student);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("未找到匹配的学生。");
                }
            }
            else
            {
                Console.WriteLine("输入为空,请输入有效的学生姓名片段。");
            }
        }
    }
}

我试过重写代码、添加索引校验方法,也咨询过GPT,但都没能解决问题,恳请各位帮忙!

内容的提问来源于stack exchange,提问作者Joshua Encarnacion

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 14:04:53