• 1404/12/08

جلسه 17 : تمرین for :

درود بر شما استاد گرامی.
امید وارم حال دلتون خوب باشه.
بنده تازه دوره رو تهیه کردم و این کد مربوط به تمرین جلسه 17 می باشد.


مراحل برنامه :

1-گرفتن تعداد دانش‌آموزان

2-گرفتن نام هر دانش‌آموز

3-گرفتن نمره هر دانش‌آموز

4- محاسبه میانگین، کمترین و بیشترین نمره

5-نمایش همه اطلاعات با رنگ

//Only 'for'
for (; ; )
{
    try
    {
        int sum = 0;
        int max = 0;
        int min = 100;

        Console.WriteLine("Enter Student Count :");
        int studentcount = Convert.ToInt32(Console.ReadLine());
        string[] studentname = new string[studentcount];
        int[] studentscore = new int[studentcount];

        for (int i = 0; i < studentcount; i++)
        {
            Console.WriteLine($"Enter Student Name {i + 1} :");
            studentname[i] = Console.ReadLine();
        }

        for (int i = 0; i < studentcount; i++)
        {
            Console.WriteLine($"Enter Student {studentname[i]} Score {i + 1} :");
            studentscore[i] = Convert.ToInt32(Console.ReadLine());

            sum += studentscore[i];

            if (studentscore[i] > max)
            {
                max = studentscore[i];
            }

            if (studentscore[i] < min)
            {
                min = studentscore[i];
            }

            if (studentscore[i] > 20 || studentscore[i] < 0)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"This Number {studentscore[i]} Not Exist ! [Must Be in Range 1-20]");
                continue;
            }

        }

        Console.WriteLine("----------------------------------");

        for (int i = 0; i < studentcount; i++)
        {
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine($"Student Name is {studentname[i]} &amp; Score is {studentscore[i]}");
        }

        Console.WriteLine("----------------------------------");
        double average = (double)sum / studentcount;
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine($"Average is {average}");
        Console.WriteLine($"High Score is {max}");
        Console.WriteLine($"Low Score is {min}");

        break;
    }

    catch
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("Please Enter Number Not Char !");
        Console.WriteLine("Try Again.....");

    }

    finally
    {
        Console.ReadKey();
        Console.ResetColor();
    }
}
  • 1404/12/08
  • ساعت 17:39

بسیار عالی بود


logo-enamadlogo-samandehi