Как исправить исключение "System.ArgumentOutOfRangeException"? - C#
Формулировка задачи:
Говорит, что ошибки в 119 и 168 строках
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication3
- {
- class Student
- {
- public string FirstName;
- public string LastName;
- public string Group;
- public List<int> Mark = new List<int>();
- public int Year;
- public string Faculty;
- public int CountOf2;
- }
- class Program
- {
- static string probel = " ";
- static List<Student> Base = new List<Student>();
- static List<Student> Base2 = new List<Student>();
- static System.IO.FileStream _File = new System.IO.FileStream("Base.txt", System.IO.FileMode.OpenOrCreate);
- static System.IO.StreamWriter fstr_out = new System.IO.StreamWriter(_File);
- static void InStudent()
- {
- Console.Clear();
- Student a = new Student();
- a.CountOf2=0;
- Console.Write("Введите имя : ");
- a.FirstName = Console.ReadLine();
- Console.Write("Введите фамилию : ");
- a.LastName = Console.ReadLine();
- Console.Write("Введите название группы : ");
- a.Group = Console.ReadLine();
- Console.Write("Введите год : ");
- a.Year = Convert.ToInt32(Console.ReadLine());
- Console.Write("Введите факультет : ");
- a.Faculty = Console.ReadLine();
- Base.Add(a);
- }
- static void OutStudent()
- {
- Console.Clear();
- Student a = new Student();
- for (int i = 0; i < Base.Count; i++)
- {
- a = Base[i];
- Console.Write(a.FirstName);
- Console.Write(probel);
- Console.Write(a.LastName);
- Console.Write(probel);
- Console.Write(a.Group);
- Console.Write(probel);
- Console.Write(a.Year);
- Console.Write(probel);
- Console.Write(a.Faculty);
- Console.Write(probel);
- Console.WriteLine();
- }
- Console.ReadKey();
- }
- static void OutStudent2()
- {
- Console.Clear();
- Student a = new Student();
- for (int i = 0; i < Base2.Count; i++)
- {
- a = Base2[i];
- Console.Write(a.FirstName);
- Console.Write(probel);
- Console.Write(a.LastName);
- Console.Write(probel);
- Console.Write(a.Group);
- Console.Write(probel);
- Console.Write(a.Year);
- Console.Write(probel);
- Console.Write(a.Faculty);
- Console.Write(probel);
- Console.WriteLine();
- }
- Console.ReadKey();
- }
- static void Menu()
- {
- string b;
- do
- {
- Console.Clear();
- Console.WriteLine("1 – Добавить студента");
- Console.WriteLine("2 – Удалить студента");
- Console.WriteLine("3 – Просмотреть весь список");
- Console.WriteLine("4 - Выход");
- b = Console.ReadLine();
- switch (b)
- {
- case "1": Program.InStudent(); break;
- case "2":
- {
- int index;
- Console.WriteLine("Введите номер студента, которого Вы хотите удалить (max - {0})", Base.Count);
- index = Convert.ToInt32(Console.Read());
- Base.RemoveAt(index);
- }; break;
- case "3": OutStudent(); break;
- case "4": {
- Console.Clear();
- for (int i = 0; i < Base.Count; i++)
- if ((Base[i].CountOf2 >= 2) && (Base[i].Faculty == "КИТ"))
- {
- Base2.Add(Base[i]);
- Base.RemoveAt(i);
- }
- else
- {
- try
- {
- fstr_out.Write(Base[i].FirstName);
- fstr_out.Write(Base[i].LastName);
- fstr_out.Write(Base[i].Group);
- fstr_out.Write(Base[i].Year);
- fstr_out.Write(Base[i].Faculty);
- }
- catch (Exception exc)
- {
- Console.WriteLine(exc.Message + "Ошибка при работе с файлом.");
- return;
- }
- };
- Console.Clear();
- Console.WriteLine("Удаленные студенты:");
- OutStudent2();
- Console.ReadKey();
- OutStudent();
- Console.ReadKey();
- }; break;
- }
- } while (!(b == "5"));
- _File.Close();
- }
- static void Main(string[] args)
- {
- Menu();
- }
- }
- }
Решение задачи: «Как исправить исключение "System.ArgumentOutOfRangeException"?»
textual
Листинг программы
- index = Convert.ToInt32(Console.ReadLine());
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д