Как исправить исключение "System.ArgumentOutOfRangeException"? - C#

Узнай цену своей работы

Формулировка задачи:

Говорит, что ошибки в 119 и 168 строках

Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ConsoleApplication3
  6. {
  7. class Student
  8. {
  9. public string FirstName;
  10. public string LastName;
  11. public string Group;
  12. public List<int> Mark = new List<int>();
  13. public int Year;
  14. public string Faculty;
  15. public int CountOf2;
  16. }
  17.  
  18. class Program
  19. {
  20. static string probel = " ";
  21. static List<Student> Base = new List<Student>();
  22. static List<Student> Base2 = new List<Student>();
  23. static System.IO.FileStream _File = new System.IO.FileStream("Base.txt", System.IO.FileMode.OpenOrCreate);
  24. static System.IO.StreamWriter fstr_out = new System.IO.StreamWriter(_File);
  25. static void InStudent()
  26. {
  27. Console.Clear();
  28. Student a = new Student();
  29. a.CountOf2=0;
  30. Console.Write("Введите имя : ");
  31. a.FirstName = Console.ReadLine();
  32. Console.Write("Введите фамилию : ");
  33. a.LastName = Console.ReadLine();
  34. Console.Write("Введите название группы : ");
  35. a.Group = Console.ReadLine();
  36. Console.Write("Введите год : ");
  37. a.Year = Convert.ToInt32(Console.ReadLine());
  38. Console.Write("Введите факультет : ");
  39. a.Faculty = Console.ReadLine();
  40. Base.Add(a);
  41. }
  42. static void OutStudent()
  43. {
  44. Console.Clear();
  45. Student a = new Student();
  46. for (int i = 0; i < Base.Count; i++)
  47. {
  48. a = Base[i];
  49. Console.Write(a.FirstName);
  50. Console.Write(probel);
  51. Console.Write(a.LastName);
  52. Console.Write(probel);
  53. Console.Write(a.Group);
  54. Console.Write(probel);
  55. Console.Write(a.Year);
  56. Console.Write(probel);
  57. Console.Write(a.Faculty);
  58. Console.Write(probel);
  59. Console.WriteLine();
  60. }
  61. Console.ReadKey();
  62. }
  63.  
  64. static void OutStudent2()
  65. {
  66. Console.Clear();
  67. Student a = new Student();
  68. for (int i = 0; i < Base2.Count; i++)
  69. {
  70. a = Base2[i];
  71. Console.Write(a.FirstName);
  72. Console.Write(probel);
  73. Console.Write(a.LastName);
  74. Console.Write(probel);
  75. Console.Write(a.Group);
  76. Console.Write(probel);
  77. Console.Write(a.Year);
  78. Console.Write(probel);
  79. Console.Write(a.Faculty);
  80. Console.Write(probel);
  81. Console.WriteLine();
  82. }
  83. Console.ReadKey();
  84. }
  85. static void Menu()
  86. {
  87. string b;
  88. do
  89. {
  90. Console.Clear();
  91. Console.WriteLine("1 – Добавить студента");
  92. Console.WriteLine("2 – Удалить студента");
  93. Console.WriteLine("3 – Просмотреть весь список");
  94. Console.WriteLine("4 - Выход");
  95. b = Console.ReadLine();
  96. switch (b)
  97. {
  98. case "1": Program.InStudent(); break;
  99. case "2":
  100. {
  101. int index;
  102. Console.WriteLine("Введите номер студента, которого Вы хотите удалить (max - {0})", Base.Count);
  103. index = Convert.ToInt32(Console.Read());
  104. Base.RemoveAt(index);
  105. }; break;
  106. case "3": OutStudent(); break;
  107. case "4": {
  108. Console.Clear();
  109. for (int i = 0; i < Base.Count; i++)
  110. if ((Base[i].CountOf2 >= 2) && (Base[i].Faculty == "КИТ"))
  111. {
  112. Base2.Add(Base[i]);
  113. Base.RemoveAt(i);
  114. }
  115. else
  116. {
  117. try
  118. {
  119. fstr_out.Write(Base[i].FirstName);
  120. fstr_out.Write(Base[i].LastName);
  121. fstr_out.Write(Base[i].Group);
  122. fstr_out.Write(Base[i].Year);
  123. fstr_out.Write(Base[i].Faculty);
  124. }
  125. catch (Exception exc)
  126. {
  127. Console.WriteLine(exc.Message + "Ошибка при работе с файлом.");
  128. return;
  129. }
  130. };
  131. Console.Clear();
  132. Console.WriteLine("Удаленные студенты:");
  133. OutStudent2();
  134. Console.ReadKey();
  135. OutStudent();
  136. Console.ReadKey();
  137. }; break;
  138. }
  139.  
  140. } while (!(b == "5"));
  141. _File.Close();
  142. }
  143.  
  144. static void Main(string[] args)
  145. {
  146. Menu();
  147. }
  148. }
  149. }

Решение задачи: «Как исправить исключение "System.ArgumentOutOfRangeException"?»

textual
Листинг программы
  1. index = Convert.ToInt32(Console.ReadLine());

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

13   голосов , оценка 4.231 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы