Example #1
0
        static void Main(string[] args)
        {
            Exam   exam = new Exam();
            String url  = "se3314.txt"; // Change URL to location of text file that contains questions

            Console.WindowWidth = 100;  // 0 < Console.WindowWidth <= Console.LargestWindowWidth
            Console.Title       = "Multiple Choice Exam";

            // Opening message
            if (!exam.LoadQuestions(url))
            {
                Console.WriteLine("\nPress any key to quit...");
                Console.ReadKey();
                return;
            }
            else
            {
                Console.WriteLine("Loaded multiple choice problems from:\n" + url + "\n");
            }

            // Iterate through questions
            while (exam.MoveNext())
            {
                WriteHorizontalRule();

                BaseQuestion question = exam.CurrentQuestion;
                question.WriteQuestion();

                int choice = 0;

                // Validate input
                while (!int.TryParse(Console.ReadLine(), out choice) || choice < 1 || choice > exam.CurrentQuestion.NumChoices)
                {
                    Console.WriteLine("Not a valid choice. Try again");
                }

                // Check answer
                if (exam.CheckAnswer(choice))
                {
                    Console.WriteLine("\nCorrect! (" + exam.Average + "% " + exam.QuestionsCorrect + "/" + exam.QuestionsCompleted + ")\n");
                }
                else
                {
                    Console.WriteLine("\nWrong! (" + exam.Average + "% " + exam.QuestionsCorrect + "/" + exam.QuestionsCompleted
                                      + ")\nCorrect answer is: " + question.Answer + "\n");
                }
            }

            WriteHorizontalRule();

            // Finish exam
            Console.WriteLine("You finished with " + exam.Average + "%\nPress any key to quit...");
            Console.ReadKey();
        }
Example #2
0
        public bool MoveNext()
        {
            if (this.questions.Count > 0)
            {
                this.questionsCompleted++;
                this.currentQuestion = this.questions.PickRandom();
                this.questions.Remove(this.currentQuestion);

                return(true);
            }

            return(false);
        }
        public static List <BaseQuestion> LoadQuestions(String url)
        {
            try
            {
                using (StreamReader streamReader = new StreamReader(url))
                {
                    String line = "";
                    List <BaseQuestion> questions = new List <BaseQuestion>();
                    BaseQuestion        question  = new BaseQuestion();

                    while ((line = streamReader.ReadLine()) != null)
                    {
                        if (line.StartsWith("QUESTION:"))
                        {
                            line = line.Substring(line.IndexOf(':') + 1);

                            question          = new BaseQuestion();
                            question.Question = line;
                        }
                        else if (line.StartsWith("ANSWER:"))
                        {
                            line = line.Substring(line.IndexOf(':') + 1);

                            question.Answer = line;
                            questions.Add(question);
                        }
                        else if (line.Contains(')'))
                        {
                            line = line.Substring(line.IndexOf(')') + 1);

                            question.AddChoice(line);
                        }
                    }

                    return(questions);
                }
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
            }

            return(null);
        }