اذهب إلى المحتوى

السؤال

نشر
using System;
namespace program
{
    class program1
    {
        static void DisplayMenus()    //Function to display menu
        {
            Console.WriteLine();
            Console.WriteLine("............Menu............");
            Console.WriteLine("1..Display percentage of correct answers..");
            Console.WriteLine("2..Display generated words for each question..");
            Console.WriteLine("3..Display user answers..");
            Console.WriteLine("4..Display correct answers..");
            Console.WriteLine("5..Display both answers..");
            Console.WriteLine("Enter choice: ");

        }

        static void Displaypercentage(int[] correctanswer_count, int num_question) //Funtion to display percentage for correct answers 
        {
            Console.WriteLine();
            int answercount = 4;
            int totalcorrect = 0;
            for (int i = 0; i < num_question; i++)
            {
                totalcorrect += correctanswer_count[i];
            }
            double percentage = ((double)totalcorrect / (num_question * answercount)) * 100;
            Console.WriteLine(" percentage :  " + percentage + "%");
        }
        static void Display_GeneratedWords(string[] generated_Array, int num_question)  //Function to display generated words for each question
        {


            Console.WriteLine("\t..Generated Words for all question..");
            for (int i = 0; i < num_question; i++)
            {
                if (i < generated_Array.Length)
                    Console.WriteLine("question{0} : genereted words: {1}\t", (i + 1), generated_Array[i]);
            }

        }
        static void Display_StudentAnswers(string[] studentsum_answer, string[] studentvowels_answer, string[] studentlower_answer, string[] studentspecial_answer, int num_question)//Function to display student answers
        {
            Console.WriteLine();
            for (int i = 0; i < num_question; i++)
            {
                if (studentsum_answer[i].ToLower() == "ignore")
                {
                    Console.WriteLine("question{0}  {1}", (i + 1), "ignore");

                }
                else
                {

                    Console.WriteLine("...question (" + (i + 1) + ")\tstudentsum_answer:\t" + studentsum_answer[i]);
                    Console.WriteLine("...question (" + (i + 1) + ")\tstudentvowels_answer:\t" + studentvowels_answer[i]);
                    Console.WriteLine("...question (" + (i + 1) + ")\tstudentlower_answer:\t" + studentlower_answer[i]);
                    Console.WriteLine("...question (" + (i + 1) + ")\tstudentspecial_answer:\t" + studentspecial_answer[i]);


                }

            }
        }
        static void Display_correctAnswers(int[] sumlength, int[] correctvowelscount, int[] correctlowercount, int[] correctspecialcount, int num_question)//Function to display correct answers
        {
            Console.WriteLine();

            for (int i = 0; i < num_question; i++)
            {
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrect sum:\t" + sumlength[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrect vowels:\t" + correctvowelscount[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrect lower:\t" + correctlowercount[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrect special:\t" + correctspecialcount[i]);

            }

        }
        static void Display_BothAnswer(string[] studentsum, string[] studentvowels, string[] studentlower, string[] studentspecial, int[] correctsum, int[] correctvowels, int[] correctlower, int[] correctspecial, int num_question)//Function to display correct answers and student answers
        {
            Console.WriteLine();
            string[] status = new string[num_question];
            for (int i = 0; i < num_question; i++)
            {


                Console.WriteLine("...question" + (i + 1) + ")...\tstudentsum_answer:\t" + studentsum[i]);
                Console.WriteLine("...question" + (i + 1) + ")...\tstudentvowels_answer:\t" + studentvowels[i]);
                Console.WriteLine("...question" + (i + 1) + ")...\tstudentlower_answer:\t" + studentlower[i]);
                Console.WriteLine("...question" + (i + 1) + ")...\tstudentspecial_answer:\t" + studentspecial[i]);

                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrectsum:\t" + correctsum[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrectvowels:\t" + correctvowels[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrectlower:\t" + correctlower[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrectspecial:\t" + correctspecial[i]);

                Console.WriteLine(" ");

            }
        }

        static void Main(string[] args)
        {

            // print laptop system information
            Console.WriteLine("Windows verison", Environment.OSVersion);
            Console.WriteLine("64 Bit operating system", Environment.Is64BitOperatingSystem ? "Yes" : "No");
            Console.WriteLine("PC Name:{0}", Environment.MachineName);
            Console.WriteLine("Number of CPUS:{0}", Environment.ProcessorCount);
            Console.WriteLine("Windows folder:{0}", Environment.SystemDirectory);
            Console.WriteLine("Logical Drivers Avaliable:{0}", string.Join(',', Environment.GetLogicalDrives()).TrimEnd(',', ' ').Replace("\\", string.Empty));
            // enter number of question 
            int num_question = 0;


            do
            {

                Console.WriteLine("please enter number of question");
                num_question = int.Parse(Console.ReadLine());
                if (num_question <= 0)
                    Console.WriteLine("the number of question must be a positive integer >0, please enter again:");
            } while (num_question <= 0);

            //Request user(student) to enter information
            string student_char = "";
            string distin_char = "";
            int acceptcount;
            do
            {
                Console.WriteLine(" please enter your information(your name and SVU ID) ");
                student_char = Console.ReadLine();
                acceptcount = 0;
                for (int i = 0; i < student_char.Length; i++)
                {
                    char ch = student_char[i];
                    if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9'))

                        acceptcount++;

                }
                // storing distinctive characters
                for (int j = 0; j < student_char.Length; j++)
                {
                    char ch = student_char[j];
                    if ((ch != ' ') && (distin_char.IndexOf(ch) == -1))
                        distin_char = distin_char + ch;

                }


                if (acceptcount >= 6) break;
                Console.WriteLine(" fault!! the acceptcount must be >=6 please enter again");

            } while (acceptcount < 6);
            Console.WriteLine("your name and svu id :" + student_char);

            Console.WriteLine("distinchar are :" + distin_char);


            // Random Question Generation
            Random number = new Random();
            string generated_word = "";
            int lowercount = 0;
            int vowelscount = 0;
            int specialcount = 0;
            int sumlength = 0;
            string[] Generated_Array = new string[num_question];
            int[] correctvowels_Array = new int[num_question];
            int[] correctlower_Array = new int[num_question];
            int[] correctspecial_Array = new int[num_question];
            int[] correctSum_Array = new int[num_question];
            string[] studentsum_answer = new string[num_question];
            string[] studentvowel_answer = new string[num_question];
            string[] studentlower_answer = new string[num_question];
            string[] studentspecial_answer = new string[num_question];
            int[] evaluation = new int[num_question];
            int[] correctanswer_count = new int[num_question];
            char[] vowelschar = { 'A', 'O', 'E', 'I', 'U', 'a', 'o', 'e', 'i', 'u' };
            char[] specialchar = { ';', '?', '!', '#' };
            int[] Sumlength_Array = new int[num_question];
            int[] lower_Array = new int[num_question];
            int[] Vowels_Array = new int[num_question];
            int[] special_Array = new int[num_question];
            int correctcount = 0;
            for (int i = 0; i < num_question; i++)
            {
                Console.WriteLine(" ******Question{0}******", (i + 1));


                int wordscount = number.Next(4, 9);  //generated words between 4 and 8
                generated_word = "";
                // generated characters for each word 

                for (int j = 0; j < wordscount; j++)
                {
                    int wordslength = number.Next(5, 11); //wordlength between 5 and 10

                    // add first letter (the first letter must be a capital or small letter)


                    sumlength += wordslength;
                    if (number.Next(2) == 0)
                    {
                        generated_word += (char)number.Next('A', 'Z' + 1);
                    }
                    else
                    {
                        generated_word += (char)number.Next('a', 'z' + 1);

                    }
                    // add the rest of the letters(capital and small letters,numbers and special letters)

                    for (int k = 1; k < wordslength; k++)
                    {
                        int choice = number.Next(5);
                        if (choice == 0)

                        {
                            generated_word += (char)number.Next('A', 'Z' + 1);

                        }
                        else if (choice == 1)
                        {
                            generated_word += (char)number.Next('a', 'z' + 1);
                            lowercount++;

                        }
                        else if (choice == 2)
                        {
                            generated_word += (char)number.Next('0', '9' + 1);

                        }
                        else if (choice == 3)
                        {

                            generated_word += vowelschar[number.Next(vowelschar.Length)];
                            vowelscount++;
                        }
                        else
                        {

                            generated_word += specialchar[number.Next(specialchar.Length)];
                            specialcount++;

                        }


                    }
                    generated_word += "  ";
                }

                Generated_Array[i] = generated_word;
                Sumlength_Array[i] = sumlength;
                Vowels_Array[i] = vowelscount;
                lower_Array[i] = lowercount;
                special_Array[i] = specialcount;
                Console.WriteLine("Generated words:    " + generated_word);
                Console.WriteLine("enter sum of word lengths or type ignore:");

                string student_answer1 = Console.ReadLine();

                if (student_answer1.ToLower() == "ignore")
                {                                        //for any question,the user can request to ignore the question without proceeding

                    studentsum_answer[i] = "ignore";
                    studentvowel_answer[i] = "ignore";
                    studentlower_answer[i] = "ignore";
                    studentspecial_answer[i] = "ignore";
                    evaluation[i] = 0;
                    correctanswer_count[i] = 0;
                    continue;
                }
                studentsum_answer[i] = student_answer1;


                Console.WriteLine("enter number of vowels words:");
                string student_answer2 = Console.ReadLine();
                studentvowel_answer[i] = student_answer2;

                Console.WriteLine("enter number of lowerletters words:");
                string student_answer3 = Console.ReadLine();
                studentlower_answer[i] = student_answer3;


                Console.WriteLine("enter number of specialletter words:");
                string student_answer4 = Console.ReadLine();
                studentspecial_answer[i] = student_answer4;


                int totalanswer = 4;
                if (int.Parse(student_answer1) == Sumlength_Array[i])
                {
                    correctcount++;
                  

                }
                if (int.Parse(student_answer2) == Vowels_Array[i])
                {
                    correctcount++;
                   

                }
                if (int.Parse(student_answer3) == lower_Array[i])
                {
                    correctcount++;
                    

                }
                if (int.Parse(student_answer4) == special_Array[i])
                {
                    correctcount++;
                    

                }
                correctanswer_count[i] = correctcount;
                correctSum_Array[i] = Sumlength_Array[i];
                correctvowels_Array[i] =Vowels_Array[i];
                correctlower_Array[i] = lower_Array[i];
                correctspecial_Array[i] = special_Array[i];

                Console.WriteLine("correct answer count= " + correctcount);
                //Evaluation(0:all answers false ,1: all answers true ,2:at least a false answer and atrue answer)
                if (correctcount == totalanswer) evaluation[i] = 1;
                else if (correctcount == 0) evaluation[i] = 0;
                else evaluation[i] = 2;
                Console.WriteLine("evaluation " + evaluation[i]);

            }

            // The second section
            // for display menu and calling the required Functions
            string Menuchoice = "";
            while (Menuchoice.ToLower() != "quit")
            {

                DisplayMenus();
                Menuchoice = Console.ReadLine();
                if (Menuchoice == "1")
                    Displaypercentage(correctanswer_count, num_question);
                else if (Menuchoice == "2")
                {
                    Display_GeneratedWords(Generated_Array, num_question);
                }
                else if (Menuchoice == "3")
                {
                    Display_StudentAnswers(studentsum_answer, studentvowel_answer, studentlower_answer, studentspecial_answer, num_question);
                }
                else if (Menuchoice == "4")
                {
                    Display_correctAnswers(correctanswer_count, correctvowels_Array, correctlower_Array, correctspecial_Array, num_question);
                }
                else if (Menuchoice == "5")
                {
                    Display_BothAnswer(studentsum_answer, studentvowel_answer, studentlower_answer, studentspecial_answer, correctanswer_count, correctvowels_Array, correctlower_Array, correctspecial_Array, num_question);
                }
                else
                {
                    Console.WriteLine("Invalid choice ");
                }

            }


        }
    
    }
}

 

Recommended Posts

  • 0
نشر

لا توجد أخطاء Syntax  تمنعه من التشغيل في إصدارات .NET الحديثة، ولكنه يحتوي على 6 أخطاء منطقية وبرمجي ستؤدي إلى إخراج نتائج خاطئة أثناء التنفيذ.

المتغيرات sumlength و vowelscount و lowercount و specialcount و correctcount تم تعريفها خارج حلقة الأسئلة for (int i = 0; i < num_question; i++) دون إعادة تصفيرها داخل الحلقة، بالتالي قيم السؤال الأول تتراكم مع السؤال الثاني والثالث، فتظهر النتائج والنسب المئوية خاطئة.

يجب تصفير المتغيرات في بداية حلقة for الخاصة بالأسئلة:

sumlength = 0;
vowelscount = 0;
lowercount = 0;
specialcount = 0;
correctcount = 0;

وأثناء توليد الكلمات العشوائية الحرف الأول من كل كلمة يتم توليده عشوائياً بين كبير وصغير، ولكن لا يتم فحصه هل هو حرف صغير لزيادة lowercount، ولا يتم فحصه هل هو حرف صوتي لزيادة vowelscount.

في باقي أحرف الكلمة، عندما يختار البرنامج choice == 0 (حرف كبير) أو choice == 1 (حرف صغير)، قد يولد حرفاً صوتياً بالصدفة مثل A أو e دون أن يزيد عداد vowelscount وعندما يختار choice == 3 (حرف صوتي)، قد يختار حرفاً صوتياً صغيراً مثل a أو u دون أن يزيد عداد lowercount.

الأصح هو توليد الكلمة أولاً، أو فحص الحرف المولد الفعلي بعد إضافته لمعرفة نوعه بدقة.

وعند استدعاء الدالتين Display_correctAnswers و Display_BothAnswer في نهاية الكود، قمت بتمرير المصفوفة correctanswer_count وهي عدد الإجابات الصحيحة بدلاً من مصفوفة مجموع الأطوال الصحيح Sumlength_Array أو correctSum_Array.

ولو كتب الطالب ignore، فالكود ينفذ continue قبل نسخ القيم إلى correctSum_Array وباقي مصفوفات الإجابات الصحيحة، فتبقى قيمها أصفاراً، لذلك اعتمد مباشرة على المصفوفات الأساسية (Sumlength_Array, Vowels_Array, lower_Array, special_Array) التي تم حفظ القيم فيها قبل سؤال الطالب.

ونسيت رمز {0} في طباعة معلومات النظام بداية البرنامج في أول سطرين داخل Main، ولن يتم طباعة إصدار الويندوز ولا نوع النظام لعدم وجود {0} داخل النص.

وعدم تصفير متغير distin_char عند إعادة إدخال بيانات الطالب إذا أدخل الطالب اسماً ورقماً جامعيّاً أقل من 6 محارف مقبولة، يطلب منه البرنامج الإدخال مرة أخرى، لكن المتغير distin_char يحتفظ بالمحارف القديمة من المحاولة الخاطئة.

عليك إضافة distin_char = ""; في بداية حلقة do-while بجانب acceptcount = 0;

وعندما يكتب المستخدم quit لإغلاق البرنامج، يدخل الكود في جملة else ويطبع Invalid choice قبل أن يتوقف، كما أن القائمة لا توضح للمستخدم خيار الخروج، قم بإضافة else if (Menuchoice.ToLower() == "quit") أو استخدام break;.

بعد التصحيح:

using System;

namespace program
{
    class program1
    {
        static void DisplayMenus()    
        {
            Console.WriteLine();
            Console.WriteLine("............Menu............");
            Console.WriteLine("1..Display percentage of correct answers..");
            Console.WriteLine("2..Display generated words for each question..");
            Console.WriteLine("3..Display user answers..");
            Console.WriteLine("4..Display correct answers..");
            Console.WriteLine("5..Display both answers..");
            Console.WriteLine("Type 'quit' to exit.");
            Console.WriteLine("Enter choice: ");
        }

        static void Displaypercentage(int[] correctanswer_count, int num_question) 
        {
            Console.WriteLine();
            int answercount = 4;
            int totalcorrect = 0;
            for (int i = 0; i < num_question; i++)
            {
                totalcorrect += correctanswer_count[i];
            }
            double percentage = ((double)totalcorrect / (num_question * answercount)) * 100;
            Console.WriteLine(" percentage :  " + percentage + "%");
        }

        static void Display_GeneratedWords(string[] generated_Array, int num_question)  
        {
            Console.WriteLine("\t..Generated Words for all question..");
            for (int i = 0; i < num_question; i++)
            {
                if (i < generated_Array.Length)
                    Console.WriteLine("question{0} : generated words: {1}\t", (i + 1), generated_Array[i]);
            }
        }

        static void Display_StudentAnswers(string[] studentsum_answer, string[] studentvowels_answer, string[] studentlower_answer, string[] studentspecial_answer, int num_question) // Function to display student answers
        {
            Console.WriteLine();
            for (int i = 0; i < num_question; i++)
            {
                if (studentsum_answer[i].ToLower() == "ignore")
                {
                    Console.WriteLine("question{0}  {1}", (i + 1), "ignore");
                }
                else
                {
                    Console.WriteLine("...question (" + (i + 1) + ")\tstudentsum_answer:\t" + studentsum_answer[i]);
                    Console.WriteLine("...question (" + (i + 1) + ")\tstudentvowels_answer:\t" + studentvowels_answer[i]);
                    Console.WriteLine("...question (" + (i + 1) + ")\tstudentlower_answer:\t" + studentlower_answer[i]);
                    Console.WriteLine("...question (" + (i + 1) + ")\tstudentspecial_answer:\t" + studentspecial_answer[i]);
                }
            }
        }

        static void Display_correctAnswers(int[] sumlength, int[] correctvowelscount, int[] correctlowercount, int[] correctspecialcount, int num_question) 
        {
            Console.WriteLine();
            for (int i = 0; i < num_question; i++)
            {
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrect sum:\t" + sumlength[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrect vowels:\t" + correctvowelscount[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrect lower:\t" + correctlowercount[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrect special:\t" + correctspecialcount[i]);
            }
        }

        static void Display_BothAnswer(string[] studentsum, string[] studentvowels, string[] studentlower, string[] studentspecial, int[] correctsum, int[] correctvowels, int[] correctlower, int[] correctspecial, int num_question) 
        {
            Console.WriteLine();
            for (int i = 0; i < num_question; i++)
            {
                Console.WriteLine("...question (" + (i + 1) + ")...\tstudentsum_answer:\t" + studentsum[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tstudentvowels_answer:\t" + studentvowels[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tstudentlower_answer:\t" + studentlower[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tstudentspecial_answer:\t" + studentspecial[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrectsum:\t" + correctsum[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrectvowels:\t" + correctvowels[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrectlower:\t" + correctlower[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrectspecial:\t" + correctspecial[i]);
                Console.WriteLine(" ");
            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Windows version: {0}", Environment.OSVersion);
            Console.WriteLine("64 Bit operating system: {0}", Environment.Is64BitOperatingSystem ? "Yes" : "No");
            Console.WriteLine("PC Name: {0}", Environment.MachineName);
            Console.WriteLine("Number of CPUS: {0}", Environment.ProcessorCount);
            Console.WriteLine("Windows folder: {0}", Environment.SystemDirectory);
            Console.WriteLine("Logical Drives Available: {0}", string.Join(",", Environment.GetLogicalDrives()).TrimEnd(',', ' ').Replace("\\", string.Empty));

            int num_question = 0;
            do
            {
                Console.WriteLine("please enter number of question");
                num_question = int.Parse(Console.ReadLine());
                if (num_question <= 0)
                    Console.WriteLine("the number of question must be a positive integer >0, please enter again:");
            } while (num_question <= 0);

            string student_char = "";
            string distin_char = "";
            int acceptcount;
            do
            {
                Console.WriteLine(" please enter your information(your name and SVU ID) ");
                student_char = Console.ReadLine();
                acceptcount = 0;
                distin_char = ""; 

                for (int i = 0; i < student_char.Length; i++)
                {
                    char ch = student_char[i];
                    if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9'))
                        acceptcount++;
                }
                for (int j = 0; j < student_char.Length; j++)
                {
                    char ch = student_char[j];
                    if ((ch != ' ') && (distin_char.IndexOf(ch) == -1))
                        distin_char = distin_char + ch;
                }

                if (acceptcount >= 6) break;
                Console.WriteLine(" fault!! the acceptcount must be >=6 please enter again");
            } while (acceptcount < 6);

            Console.WriteLine("your name and svu id :" + student_char);
            Console.WriteLine("distinchar are :" + distin_char);

            Random number = new Random();
            string[] Generated_Array = new string[num_question];
            string[] studentsum_answer = new string[num_question];
            string[] studentvowel_answer = new string[num_question];
            string[] studentlower_answer = new string[num_question];
            string[] studentspecial_answer = new string[num_question];
            int[] evaluation = new int[num_question];
            int[] correctanswer_count = new int[num_question];
            char[] vowelschar = { 'A', 'O', 'E', 'I', 'U', 'a', 'o', 'e', 'i', 'u' };
            char[] specialchar = { ';', '?', '!', '#' };
            int[] Sumlength_Array = new int[num_question];
            int[] lower_Array = new int[num_question];
            int[] Vowels_Array = new int[num_question];
            int[] special_Array = new int[num_question];

            for (int i = 0; i < num_question; i++)
            {
                Console.WriteLine(" ******Question{0}******", (i + 1));

                string generated_word = "";
                int lowercount = 0;
                int vowelscount = 0;
                int specialcount = 0;
                int sumlength = 0;
                int correctcount = 0;

                int wordscount = number.Next(4, 9);  
                for (int j = 0; j < wordscount; j++)
                {
                    int wordslength = number.Next(5, 11); 
                    sumlength += wordslength;

                    for (int k = 0; k < wordslength; k++)
                    {
                        char currentChar;
                        if (k == 0)
                        {
                            if (number.Next(2) == 0)
                                currentChar = (char)number.Next('A', 'Z' + 1);
                            else
                                currentChar = (char)number.Next('a', 'z' + 1);
                        }
                        else
                        {
                            int choice = number.Next(5);
                            if (choice == 0)
                                currentChar = (char)number.Next('A', 'Z' + 1);
                            else if (choice == 1)
                                currentChar = (char)number.Next('a', 'z' + 1);
                            else if (choice == 2)
                                currentChar = (char)number.Next('0', '9' + 1);
                            else if (choice == 3)
                                currentChar = vowelschar[number.Next(vowelschar.Length)];
                            else
                                currentChar = specialchar[number.Next(specialchar.Length)];
                        }

                        generated_word += currentChar;

                        // حساب دقيق لنوع الحرف المولد
                        if (currentChar >= 'a' && currentChar <= 'z')
                            lowercount++;
                        if (Array.IndexOf(vowelschar, currentChar) != -1)
                            vowelscount++;
                        if (Array.IndexOf(specialchar, currentChar) != -1)
                            specialcount++;
                    }
                    generated_word += "  ";
                }

                Generated_Array[i] = generated_word;
                Sumlength_Array[i] = sumlength;
                Vowels_Array[i] = vowelscount;
                lower_Array[i] = lowercount;
                special_Array[i] = specialcount;

                Console.WriteLine("Generated words:    " + generated_word);
                Console.WriteLine("enter sum of word lengths or type ignore:");
                string student_answer1 = Console.ReadLine();

                if (student_answer1.ToLower() == "ignore")
                {
                    studentsum_answer[i] = "ignore";
                    studentvowel_answer[i] = "ignore";
                    studentlower_answer[i] = "ignore";
                    studentspecial_answer[i] = "ignore";
                    evaluation[i] = 0;
                    correctanswer_count[i] = 0;
                    continue;
                }

                studentsum_answer[i] = student_answer1;

                Console.WriteLine("enter number of vowels letters:");
                string student_answer2 = Console.ReadLine();
                studentvowel_answer[i] = student_answer2;

                Console.WriteLine("enter number of lower letters:");
                string student_answer3 = Console.ReadLine();
                studentlower_answer[i] = student_answer3;

                Console.WriteLine("enter number of special letters:");
                string student_answer4 = Console.ReadLine();
                studentspecial_answer[i] = student_answer4;

                int totalanswer = 4;
                if (int.Parse(student_answer1) == Sumlength_Array[i])
                    correctcount++;
                if (int.Parse(student_answer2) == Vowels_Array[i])
                    correctcount++;
                if (int.Parse(student_answer3) == lower_Array[i])
                    correctcount++;
                if (int.Parse(student_answer4) == special_Array[i])
                    correctcount++;

                correctanswer_count[i] = correctcount;
                Console.WriteLine("correct answer count= " + correctcount);

                if (correctcount == totalanswer) evaluation[i] = 1;
                else if (correctcount == 0) evaluation[i] = 0;
                else evaluation[i] = 2;

                Console.WriteLine("evaluation " + evaluation[i]);
            }

            string Menuchoice = "";
            while (Menuchoice.ToLower() != "quit")
            {
                DisplayMenus();
                Menuchoice = Console.ReadLine();

                if (Menuchoice == "1")
                {
                    Displaypercentage(correctanswer_count, num_question);
                }
                else if (Menuchoice == "2")
                {
                    Display_GeneratedWords(Generated_Array, num_question);
                }
                else if (Menuchoice == "3")
                {
                    Display_StudentAnswers(studentsum_answer, studentvowel_answer, studentlower_answer, studentspecial_answer, num_question);
                }
                else if (Menuchoice == "4")
                {
                    Display_correctAnswers(Sumlength_Array, Vowels_Array, lower_Array, special_Array, num_question);
                }
                else if (Menuchoice == "5")
                {
                    Display_BothAnswer(studentsum_answer, studentvowel_answer, studentlower_answer, studentspecial_answer, Sumlength_Array, Vowels_Array, lower_Array, special_Array, num_question);
                }
                else if (Menuchoice.ToLower() == "quit")
                {
                    Console.WriteLine("Exiting program...");
                }
                else
                {
                    Console.WriteLine("Invalid choice ");
                }
            }
        }
    }
}

 

  • 0
نشر

العدادات لا تصفر بين الأسئلة في Main، المتغيرات sumlength وlowercount وvowelscount وspecialcount معرفة قبل حلقة الأسئلة. لذلك تستمر القيم من سؤال إلى آخر إجابة السؤال الثاني تصبح مجموع السؤالين، والثالث مجموع الثلاثة، وهكذا يجب ان تعرفها داخل حلقة for مع القيمة 0 في بدايتها

for (int i = 0; i < num_question; i++)
{
    int sumlength = 0, lowercount = 0, vowelscount = 0, specialcount = 0;
    ...
}

correctcount لا يصفر بين الأسئلة int correctcount = 0; خارج الحلقة لذلك يتراكم عدد الإجابات الصحيحة، فيصبح correctanswer_count[i] أكبر من 4 والنسبة المئوية تتجاوز 100%. شرط correctcount == totalanswer لن يتحقق إلا في السؤال الأول لذلك يجب انقله داخل الحلقة أيضًا int correctcount = 0;

تمرير مصفوفة خاطئة في القائمة 4 و5 في استدعاء الدالتين داخل القائمة 

Display_correctAnswers(correctanswer_count, ...)   // خطأ

المطلوب هنا مجموع الأطوال الصحيح، لكنك مررت correctanswer_count وهي عدد الإجابات الصحيحة لذلك مرر Sumlength_Array (أو correctSum_Array) 

Display_correctAnswers(Sumlength_Array, Vowels_Array, lower_Array, special_Array, num_question);
Display_BothAnswer(studentsum_answer, studentvowel_answer, studentlower_answer, studentspecial_answer,
                   Sumlength_Array, Vowels_Array, lower_Array, special_Array, num_question);

عد الأحرف الصغيرة غير دقيق عند توليد الحرف الأول، وعند اختيار حرف علة صغير (a, e, i, o, u) لذلك لا يزاد lowercount في هاتين الحالتين، مع أن الحرف صغير فعلًا الحل هو ان تعد الأحرف بعد انتهاء توليد النص، بالمرور على كل حرف 

foreach (char c in generated_word)
{
    if (c >= 'a' && c <= 'z') lowercount++;
    if (Array.IndexOf(vowelschar, c) >= 0) vowelscount++;
    if (Array.IndexOf(specialchar, c) >= 0) specialcount++;
}

هذا هو الكل كاملا مع التصحيح

using System;

namespace program
{
    class program1
    {
        static void DisplayMenus()
        {
            Console.WriteLine();
            Console.WriteLine("............Menu............");
            Console.WriteLine("1..Display percentage of correct answers..");
            Console.WriteLine("2..Display generated words for each question..");
            Console.WriteLine("3..Display user answers..");
            Console.WriteLine("4..Display correct answers..");
            Console.WriteLine("5..Display both answers..");
            Console.WriteLine("(type quit to exit)");
            Console.Write("Enter choice: ");
        }

        static void Displaypercentage(int[] correctanswer_count, int num_question)
        {
            Console.WriteLine();
            int answercount = 4;
            int totalcorrect = 0;
            for (int i = 0; i < num_question; i++)
                totalcorrect += correctanswer_count[i];

            double percentage = ((double)totalcorrect / (num_question * answercount)) * 100;
            Console.WriteLine(" percentage :  " + percentage.ToString("0.##") + "%");
        }

        static void Display_GeneratedWords(string[] generated_Array, int num_question)
        {
            Console.WriteLine("\t..Generated Words for all question..");
            for (int i = 0; i < num_question; i++)
                Console.WriteLine("question{0} : generated words: {1}", (i + 1), generated_Array[i]);
        }

        static void Display_StudentAnswers(string[] sum, string[] vowels, string[] lower, string[] special, int num_question)
        {
            Console.WriteLine();
            for (int i = 0; i < num_question; i++)
            {
                if (sum[i] == "ignore")
                {
                    Console.WriteLine("question{0}  ignore", (i + 1));
                }
                else
                {
                    Console.WriteLine("...question (" + (i + 1) + ")\tstudentsum_answer:\t" + sum[i]);
                    Console.WriteLine("...question (" + (i + 1) + ")\tstudentvowels_answer:\t" + vowels[i]);
                    Console.WriteLine("...question (" + (i + 1) + ")\tstudentlower_answer:\t" + lower[i]);
                    Console.WriteLine("...question (" + (i + 1) + ")\tstudentspecial_answer:\t" + special[i]);
                }
            }
        }

        static void Display_correctAnswers(int[] sum, int[] vowels, int[] lower, int[] special, int num_question)
        {
            Console.WriteLine();
            for (int i = 0; i < num_question; i++)
            {
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrect sum:\t" + sum[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrect vowels:\t" + vowels[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrect lower:\t" + lower[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrect special:\t" + special[i]);
            }
        }

        static void Display_BothAnswer(string[] studentsum, string[] studentvowels, string[] studentlower, string[] studentspecial,
                                       int[] correctsum, int[] correctvowels, int[] correctlower, int[] correctspecial, int num_question)
        {
            Console.WriteLine();
            for (int i = 0; i < num_question; i++)
            {
                Console.WriteLine("...question (" + (i + 1) + ")...\tstudentsum_answer:\t" + studentsum[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tstudentvowels_answer:\t" + studentvowels[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tstudentlower_answer:\t" + studentlower[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tstudentspecial_answer:\t" + studentspecial[i]);

                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrectsum:\t" + correctsum[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrectvowels:\t" + correctvowels[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrectlower:\t" + correctlower[i]);
                Console.WriteLine("...question (" + (i + 1) + ")...\tcorrectspecial:\t" + correctspecial[i]);
                Console.WriteLine(" ");
            }
        }

        // قراءة عدد صحيح مع إعادة السؤال عند الإدخال الخاطئ
        static int ReadInt(string message)
        {
            int value;
            Console.WriteLine(message);
            while (!int.TryParse(Console.ReadLine(), out value))
                Console.WriteLine("invalid number, please enter again:");
            return value;
        }

        static void Main(string[] args)
        {
            // System information
            Console.WriteLine("Windows version: {0}", Environment.OSVersion);
            Console.WriteLine("64 Bit operating system: {0}", Environment.Is64BitOperatingSystem ? "Yes" : "No");
            Console.WriteLine("PC Name: {0}", Environment.MachineName);
            Console.WriteLine("Number of CPUS: {0}", Environment.ProcessorCount);
            Console.WriteLine("Windows folder: {0}", Environment.SystemDirectory);
            Console.WriteLine("Logical Drives Available: {0}",
                string.Join(",", Environment.GetLogicalDrives()).TrimEnd(',', ' ').Replace("\\", string.Empty));

            // Number of questions
            int num_question;
            do
            {
                num_question = ReadInt("please enter number of question");
                if (num_question <= 0)
                    Console.WriteLine("the number of question must be a positive integer >0, please enter again:");
            } while (num_question <= 0);

            // Student information
            string student_char;
            string distin_char;
            int acceptcount;
            do
            {
                Console.WriteLine(" please enter your information(your name and SVU ID) ");
                student_char = Console.ReadLine() ?? "";
                acceptcount = 0;
                distin_char = "";   // إعادة التصفير عند كل محاولة

                for (int i = 0; i < student_char.Length; i++)
                {
                    char ch = student_char[i];
                    if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9'))
                        acceptcount++;

                    if (ch != ' ' && distin_char.IndexOf(ch) == -1)
                        distin_char += ch;
                }

                if (acceptcount < 6)
                    Console.WriteLine(" fault!! the acceptcount must be >=6 please enter again");

            } while (acceptcount < 6);

            Console.WriteLine("your name and svu id :" + student_char);
            Console.WriteLine("distinchar are :" + distin_char);

            // Arrays
            Random number = new Random();
            string[] Generated_Array = new string[num_question];
            int[] Sumlength_Array = new int[num_question];
            int[] Vowels_Array = new int[num_question];
            int[] lower_Array = new int[num_question];
            int[] special_Array = new int[num_question];

            string[] studentsum_answer = new string[num_question];
            string[] studentvowel_answer = new string[num_question];
            string[] studentlower_answer = new string[num_question];
            string[] studentspecial_answer = new string[num_question];

            int[] evaluation = new int[num_question];
            int[] correctanswer_count = new int[num_question];

            char[] vowelschar = { 'A', 'O', 'E', 'I', 'U', 'a', 'o', 'e', 'i', 'u' };
            char[] specialchar = { ';', '?', '!', '#' };

            for (int i = 0; i < num_question; i++)
            {
                Console.WriteLine(" ******Question{0}******", (i + 1));

                // العدادات تُصفَّر لكل سؤال
                string generated_word = "";
                int sumlength = 0;
                int lowercount = 0;
                int vowelscount = 0;
                int specialcount = 0;
                int correctcount = 0;

                int wordscount = number.Next(4, 9);   // 4..8 words

                for (int j = 0; j < wordscount; j++)
                {
                    int wordslength = number.Next(5, 11);   // 5..10 chars
                    sumlength += wordslength;

                    // First letter: capital or small
                    char first;
                    if (number.Next(2) == 0)
                        first = (char)number.Next('A', 'Z' + 1);
                    else
                        first = (char)number.Next('a', 'z' + 1);
                    generated_word += first;

                    // Rest of the characters
                    for (int k = 1; k < wordslength; k++)
                    {
                        int choice = number.Next(5);
                        if (choice == 0)
                            generated_word += (char)number.Next('A', 'Z' + 1);
                        else if (choice == 1)
                            generated_word += (char)number.Next('a', 'z' + 1);
                        else if (choice == 2)
                            generated_word += (char)number.Next('0', '9' + 1);
                        else if (choice == 3)
                            generated_word += vowelschar[number.Next(vowelschar.Length)];
                        else
                            generated_word += specialchar[number.Next(specialchar.Length)];
                    }
                    generated_word += "  ";
                }

                // حساب الإجابات الصحيحة بفحص النص النهائي (يشمل الحرف الأول أيضًا)
                foreach (char c in generated_word)
                {
                    if (c >= 'a' && c <= 'z') lowercount++;
                    if (Array.IndexOf(vowelschar, c) >= 0) vowelscount++;
                    if (Array.IndexOf(specialchar, c) >= 0) specialcount++;
                }

                Generated_Array[i] = generated_word;
                Sumlength_Array[i] = sumlength;
                Vowels_Array[i] = vowelscount;
                lower_Array[i] = lowercount;
                special_Array[i] = specialcount;

                Console.WriteLine("Generated words:    " + generated_word);
                Console.WriteLine("enter sum of word lengths or type ignore:");

                // السؤال الأول: رقم أو ignore
                string a1;
                int n1 = 0;
                while (true)
                {
                    a1 = (Console.ReadLine() ?? "").Trim();
                    if (a1.ToLower() == "ignore" || int.TryParse(a1, out n1)) break;
                    Console.WriteLine("invalid input, enter a number or ignore:");
                }

                if (a1.ToLower() == "ignore")
                {
                    studentsum_answer[i] = "ignore";
                    studentvowel_answer[i] = "ignore";
                    studentlower_answer[i] = "ignore";
                    studentspecial_answer[i] = "ignore";
                    evaluation[i] = 0;
                    correctanswer_count[i] = 0;
                    continue;
                }

                int n2 = ReadInt("enter number of vowels:");
                int n3 = ReadInt("enter number of lowerletters:");
                int n4 = ReadInt("enter number of specialletters:");

                studentsum_answer[i] = n1.ToString();
                studentvowel_answer[i] = n2.ToString();
                studentlower_answer[i] = n3.ToString();
                studentspecial_answer[i] = n4.ToString();

                if (n1 == sumlength) correctcount++;
                if (n2 == vowelscount) correctcount++;
                if (n3 == lowercount) correctcount++;
                if (n4 == specialcount) correctcount++;

                correctanswer_count[i] = correctcount;
                Console.WriteLine("correct answer count= " + correctcount);

                // Evaluation (0: all false, 1: all true, 2: mixed)
                if (correctcount == 4) evaluation[i] = 1;
                else if (correctcount == 0) evaluation[i] = 0;
                else evaluation[i] = 2;
                Console.WriteLine("evaluation " + evaluation[i]);
            }

            // Menu
            string Menuchoice = "";
            while (Menuchoice.ToLower() != "quit")
            {
                DisplayMenus();
                Menuchoice = (Console.ReadLine() ?? "").Trim();

                if (Menuchoice == "1")
                    Displaypercentage(correctanswer_count, num_question);
                else if (Menuchoice == "2")
                    Display_GeneratedWords(Generated_Array, num_question);
                else if (Menuchoice == "3")
                    Display_StudentAnswers(studentsum_answer, studentvowel_answer, studentlower_answer, studentspecial_answer, num_question);
                else if (Menuchoice == "4")
                    Display_correctAnswers(Sumlength_Array, Vowels_Array, lower_Array, special_Array, num_question);
                else if (Menuchoice == "5")
                    Display_BothAnswer(studentsum_answer, studentvowel_answer, studentlower_answer, studentspecial_answer,
                                       Sumlength_Array, Vowels_Array, lower_Array, special_Array, num_question);
                else if (Menuchoice.ToLower() != "quit")
                    Console.WriteLine("Invalid choice ");
            }
        }
    }
}

 

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...