M
M
Mari322019-03-04 10:42:33
C++ / C#
Mari32, 2019-03-04 10:42:33

How to output grades in a C# program?

namespace ConsoleApp1
{
    struct Student
    {
        public string Name;
        public string GroupName;
        public DateTime BirthDay;
        public Dictionary<string, byte> Marks;
        public Student(string name, string groupname, DateTime birthday)
        {
            Name = name;
            GroupName = groupname;
            BirthDay = birthday;
            Marks = new Dictionary<string, byte>();
        }
        public string ToString(bool isFullInfo)
        {
            string res = "";
                res += String.Format("{0,15} {3}.{2}.{1}r. {4}", Name, BirthDay.Year, BirthDay.Month, BirthDay.Day, GroupName);
            if (isFullInfo)
            {
                foreach (KeyValuePair<string, byte> m in Marks)
                    res += String.Format("\n{0} - {1}", m.Key, m.Value);
            }
            return res;
        }

        class Program
        {
            static void Main(string[] args)
            {
                string[] Subjects = new string[] { "Физика", "История", "Информатика" };
                List<Student> students = new List<Student>();
                Student n1 = new Student("Николай", "18-ИНН", new DateTime(2000, 02, 12));
                n1.Marks.Add("Физика", 4);
                n1.Marks.Add("История", 4);
                n1.Marks.Add("Информатика", 5);
                Student n2 = new Student("Денис", "18-ИАС", new DateTime(2000, 03, 25));
                students.Add(n2);
                n2.Marks.Add("Физика", 2);
                n2.Marks.Add("История", 4);
                n2.Marks.Add("Информатика", 4);

                Student n3 = new Student("Александр", "18-ИСТ", new DateTime(2000, 05, 19));
                students.Add(n3);
                n3.Marks.Add("Физика", 5);
                n3.Marks.Add("История", 5);
                n3.Marks.Add("Информатика", 5);

                Console.WriteLine("Меню: \n1) Список студентов \n2) Список задолжников \n3) Список студентов с повышенной стипендией");
                int menu = Convert.ToByte(Console.ReadLine());
                switch (menu)
                {
                    case 1:
                        for (int i = 0; i < students.Count; i++)
                        {
                            Console.WriteLine("Имя: {0} Группа: {1} Дата рождения: {2}", students[i].Name + "\t", students[i].GroupName + "\t", students[i].BirthDay + "\t");
                        }
                        break;
                        case 2:
                        foreach (Student s in students)
                        {
}
                        break;
                }
                Console.ReadKey();
            }

        }
    }
}

There is a program written in C#. It is required to display students and their grades for each subject. Also identify debtors and students receiving an increased scholarship. Debtors are students who have received at least one "2".

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Foggy Finder, 2019-03-04
@FoggyFinder

It's not entirely clear what caused the problem. But let's figure it out.
1. You already have a ToString method definition for your Student object . I propose to change it a little - instead of passing a variable indicating the request for information in a detailed or abbreviated form, we will divide it into two separate methods. ShortFormat , which will return the short version, while overriding the default ToString() to get the full data.

public string ShortFormat()
{
    return string.Format("{0,15} {1} {2}", Name, BirthDay.ToShortDateString(), GroupName);
}

public override string ToString()
{
    StringBuilder sb = new StringBuilder(ShortFormat());
    sb.AppendLine("\nОценки:");
    foreach (var m in Marks)
        sb.AppendFormat("{0} - {1}\n", m.Key, m.Value);
    return sb.ToString();
}

2. To determine if our student has debts, we will use the ContainsValue method : Now it remains only to correctly call the methods / properties:
switch (menu)
{
    case 1:
        foreach (var student in students)
            Console.WriteLine(student);
        break;
    case 2:
        foreach (var student in students.Where(s => s.HasDebt))
            Console.WriteLine(student.ShortFormat());
        break;
}

Note that we used the Linq Where method to filter the debtors .
And in addition a few comments:
1. Exposing a mutable collection (in your case Dictionary<_,_>) is usually not a good idea, it's better to add a few helper methods / properties that will work with a private field.
2. In the current implementation, the menu allows you to select only one option.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question