Не понимаю как ввести и вывести оценки по каждому предмету
Как ввести оценки ПР: Информатика: 3, 4, 3. Физика: 3, 4, 5. И вывести средний балл по каждому предмету и вывести сведения о тех студентах который средний балл выше 4 и подсчитать их кол-во. Не понимаю почему не выводится "stud.Print();".
Console.Write("Введите кол-во студентов: ");
int CountStudents = int.Parse(Console.ReadLine());
Students[] students = new Students[CountStudents];
for (int i = 0; i < CountStudents; i++)
{
Console.WriteLine($"\tВведите информацию о {i + 1} студенте");
Console.Write("Ф.И.О: ");
string FullName = Console.ReadLine();
Console.Write("Группа: ");
int ClassRoom = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\tОценки");
Console.Write("Информатика: ");
int Informatics = Convert.ToInt32(Console.Read()); Console.ReadLine();
Console.Write("Физика: ");
int Physics = Convert.ToInt32(Console.Read()); Console.ReadLine();
Console.Write("История: ");
int History = Convert.ToInt32(Console.Read()); Console.ReadLine();
Students stud = new(FullName, ClassRoom, Informatics, Physics, History);
stud.Print();
}
struct Students
{
public string FullName;
public int ClassRoom;
public int Informatics;
public int Physics;
public int History;
public Students(string FullName, int ClassRoom, int Informatics, int Physics, int History)
{
this.FullName = FullName;
this.ClassRoom = ClassRoom;
this.Informatics = Informatics;
this.Physics = Physics;
this.History = History;
}
public void Print()
{
Console.WriteLine($"\tВывод \nФ.И.О: {FullName} \nГруппа: {ClassRoom} \nИнформатика: {Informatics} \nФизика: {Physics} \nИстория: {History}");
}
}
Ответы (1 шт):
По порядку
struct Students Почему информация об одном студенте называется Students? Должно быть Student.
Здесь всё делаете правильно
int CountStudents = int.Parse(Console.ReadLine());
А здесь явно что-то пошло не так
int Informatics = Convert.ToInt32(Console.Read()); Console.ReadLine();
Как так вышло? Должно быть
int Informatics = Convert.ToInt32(Console.ReadLine());
Далее, вы создаете массив
Students[] students = new Students[CountStudents];
Но нигде его не заполняете. Странно. Зачем вам тогда массив?
Students stud = new();
Создает пустого студента, и вы сразу же его печатаете и удивляетесь, а почему же он пустой... Тоже странно. Как по-вашему данные попадут в структуру?
Локальные переменные принято кстати в C# называть с маленькой буквы, чтобы не путать с типами, методами и свойствами.
Вот вам код с учетом всех замечаний выше.
Console.Write("Введите кол-во студентов: ");
int countStudents = int.Parse(Console.ReadLine());
Student[] students = new Student[countStudents];
for (int i = 0; i < students.Length; i++)
{
Console.WriteLine($"\tВведите информацию о {i + 1} студенте");
Console.Write("Ф.И.О: ");
string fullName = Console.ReadLine();
Console.Write("Группа: ");
int classRoom = int.Parse(Console.ReadLine());
Console.WriteLine("\tОценки");
Console.Write("Информатика: ");
int informatics = int.Parse(Console.ReadLine());
Console.Write("Физика: ");
int physics = int.Parse(Console.ReadLine());
Console.Write("История: ");
int history = int.Parse(Console.ReadLine());
Student student = new Student(fullName, classRoom, informatics, physics, history);
students[i] = student;
}
for (int i = 0; i < students.Length; i++)
{
students[i].Print();
}
struct Student
{
public string FullName;
public int ClassRoom;
public int Informatics;
public int Physics;
public int History;
public Student(string fullName, int classRoom, int informatics, int physics, int history)
{
FullName = fullName;
ClassRoom = classRoom;
Informatics = informatics;
Physics = physics;
History = history;
}
public void Print()
{
Console.WriteLine($"\tВывод \nФ.И.О: {FullName} \nГруппа: {ClassRoom} \nИнформатика: {Informatics} \nФизика: {Physics} \nИстория: {History}");
}
}
Осталось посчитать оценки.