• 1404/12/02

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

سلام استاد روزتون بخیر و شادی

 تمرینات جلسه 17 (ساخت چند مینی پروژه تمرینی با حلقه for):

تمرین 1 - سیستم ثبت نام و وارد کردن رمز

string password=null;
string userName=null;

for (; ; )
{
    Console.WriteLine("1- Sign Up\r\n2- Log In\r\n3- Exit");
    string input = Console.ReadLine();

    switch (input)
    {
        case "1":
            {
                if (userName != null)
                {
                    Console.WriteLine("You have signed up before. Returning to main menu:");
                    continue;
                }
                else
                {
                    Console.WriteLine("Please create a username:");
                    for (; ; )
                    {
                        userName = Console.ReadLine();

                        if (userName.Length < 5)
                        {
                            Console.WriteLine("User name must have at least 5 letters. Please try again.");
                            continue;
                        }

                        Console.WriteLine($"Dear {userName}. Please create a password:");
                        for (; ; )
                        {
                            string newPassword = Console.ReadLine();

                            if (newPassword.Length < 6)
                            {
                                Console.WriteLine("Password must have at least 6 letters. Please try again.");
                                continue;
                            }

                            Console.WriteLine("Please confirm your password:");
                            password = Console.ReadLine();

                            if (password != newPassword)
                            {
                                Console.WriteLine("Password does not match. Please creat a new password:");
                                continue;
                            }
                            else
                            {
                                Console.WriteLine("Registration Successful. Returning to main menu:");
                            }
                            break;
                        }
                        break;
                    }
                }
                continue;
            }
        case "2":
            {
                if (userName == null)
                {
                    Console.WriteLine("You haven't signed up yet. Back to menu:");
                    continue;
                }
                else
                {
                    Console.WriteLine("Please enter your password to log in:");
                    for (int i = 1; i <= 3;)
                    {
                        string input2 = Console.ReadLine();
                        if (input2 == "")
                        {
                            Console.WriteLine("Password cannot be empty. Please try again.");
                        }
                        else if (input2 != password && i == 3)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("Your account has been locked due to multiple failed attempts.");
                            i++;
                        }
                        else if (input2 != password)
                        {
                            Console.WriteLine("Wrong password. You have " + (3 - i) + " attempts remaining");
                            i++;
                        }
                        else
                        {
                            Console.WriteLine("Welcome!");
                            break;
                        }
                    }
                }
                break ;
            }
        case "3":
            {
                Console.WriteLine("Goodbye.");
                break;
            }
        default:
            {
                Console.WriteLine("Invalid input. Returning to main menu:");
                continue;
            }
    }
    break;
}



تمرین 2 - دریافت اسامی و نمرات دانش آموزان و محاسبه معدل و دانش آموز برتر کلاس

double highestAverage = 0;
string bestStudent = null;
string status;
int studentCount = 0;
int gradesCount = 0;

try
{
    Console.WriteLine("Enter the number of students in the class:");
    for (int i = 0; i < 1;)
    {
        studentCount = Convert.ToInt32(Console.ReadLine());

        if (studentCount <= 0)
        {
            Console.WriteLine("the class must have at least 1 student. Please try again:");
            continue;
        }
        i++;
    }

    Console.WriteLine("Enter the number of grades per student:");

    for (int i = 0; i < 1;)
    {
        gradesCount = Convert.ToInt32(Console.ReadLine());

        if (gradesCount <= 0)
        {
            Console.WriteLine("Each student must have at least 1 grade. Please try again:");
            continue;
        }
        i++;
    }

    double[,] studentGrades = new double[studentCount, gradesCount];
    string[] studentNames = new string[studentCount];
    double[] studentAverages = new double[studentCount];

    for (int i = 0; i < studentCount; i++)
    {
        Console.WriteLine($"Enter the name of student number {i + 1}:");
        studentNames[i] = Console.ReadLine();

        double gradesTotal = 0;
        for (int j = 0; j < gradesCount; j++)
        {
            Console.WriteLine($"Enter {studentNames[i]}'s grade number {j + 1}:");
            studentGrades[i, j] = Convert.ToDouble(Console.ReadLine());

            if (studentGrades[i, j] < 0 || studentGrades[i, j] > 20)
            {
                Console.WriteLine("The grade must be between 0 and 20!");
                j--;
                continue;
            }

            gradesTotal += studentGrades[i, j];
        }
        studentAverages[i] = gradesTotal / gradesCount;
        if (studentAverages[i] > highestAverage)
        {                                                                              
            highestAverage = studentAverages[i];                                       
            bestStudent = studentNames[i];
        }
    }

    for (int i = 0; i < studentCount; i++)                      
    {
        if (studentAverages[i] >= 17)                           
        {
            Console.ForegroundColor = ConsoleColor.Green;
            status = "Excellent";
        }
        else if (studentAverages[i] >= 12)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            status = "Good";
        }
        else
        {
            Console.ForegroundColor = ConsoleColor.Red;
            status = "Failed";
        }

        Console.Write($"{studentNames[i]}:\r\nAverage = {studentAverages[i]}\r\nGrades: {studentGrades[i, 0]}");

        for (int j = 1; j < gradesCount; j++)
        {
            Console.Write($" , {studentGrades[i, j]}");
        }
        Console.WriteLine($"\r\nStatus: {status}\r\n ");
    }

    Console.ForegroundColor = ConsoleColor.Blue;

    Console.WriteLine($"{bestStudent} is the best student in the class with an average of {highestAverage}");
}
catch
{
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("Please enter just numbers!");
}
finally
{
    Console.ResetColor();
    Console.ReadKey();
}
  • 1404/12/02
  • ساعت 21:08

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


logo-enamadlogo-samandehi