1

Trying to sort my quiz program into classes but I can't figure out how to initiate them and call there contents up later on in the code. I want to add my "questions" into their own class, then call them later on, and eventually do the same with my "answers & answer choices". I had to cut out some of the code to allow it to post in this forum. Any and all help is greatly appreciated!

namespace ConsoleApp3
{
    class Questions
    {
        public Questions();
        string[] questions =    //question array
            {
            "1. What year is it in the opening scene of the movie?", //mult choice
            "2. Peter Quill’s alias is “Star Prince”", //true false

        };

        public string[] Questions { get => questions; set => questions = value; }
    }
    class Program
    {
        private const bool V = false;

        static void Main(string[] args)
        {
            int correct = 0;
            int wrong = 0;
            string quizTitle = "How well do you know the Guardians of the Galaxy?";
            string prompt = "Please make a selection...";



            string[] answerChoices =    //answer choices array
            {
            "   a. 1981\n   b. 1988\n   c. 1985\n   d. 1990",
            "   a. True\n   b. False",

        };
            string[] answers =  //correct answer array
            {
            "b",
            "b",

        };
            //array for flagging incorrect answers
            bool[] redoQuestion =
            {
            false,
            false,

        };                      //start of program

            string input;
            for (int x = 0; x < questions.Length; ++x)      //for loop to ask & check questions for initial attempt
            {
                do
                {           //checking for valid input
                    Console.WriteLine(Questions.ToString questions[x]);
                    Console.WriteLine(answerChoices[x]);
                    Console.WriteLine(prompt);
                    input = Console.ReadLine();
                } 
2
  • why would you code your questions and answers? Store them in the DB. text file, json/xml. And only let your program to load them, display them, parse the results Commented May 4, 2020 at 2:50
  • 1
    @T.S., easy... guidance is one thing... a newbie in code is another. Probably getting feet wet to understand classes, creation, use. Throw in full database crud and they might be overloaded... but CJ... T.S is correct. Once you get basics going, this type of stuff should be in a database such as one column for Q, one for A, a third for fake answers which you can randomly throw the correct answer into the list of fake. Commented May 4, 2020 at 2:53

3 Answers 3

2

There are many ways to structure this data, but here's a reasonable option:

public class Answer
{
    public string Option;
    public string Text;
}

public class Question
{
    public string Text;
    public string CorrectOption;
    public Answer[] Answers;
}

Then you initialize your data like this:

var questions = new []
{
    new Question()
    {
        Text = "What year is it in the opening scene of the movie?",
        CorrectOption = "b",
        Answers = new []
        {
            new Answer() { Option = "a", Text = "1981" },
            new Answer() { Option = "b", Text = "1988" },
            new Answer() { Option = "c", Text = "1985" },
            new Answer() { Option = "d", Text = "1990" },
        }
    },
    new Question()
    {
        Text = "Peter Quill’s alias is “Star Prince”",
        CorrectOption = "b",
        Answers = new []
        {
            new Answer() { Option = "a", Text = "True" },
            new Answer() { Option = "b", Text = "False" },
        }
    }
};

The beauty of this is that it keeps each question nicely together with the answers and an indication of what is the correct answer.

Sign up to request clarification or add additional context in comments.

Comments

0
    static void Main(string[] args)
    {
        var quiz = new Quiz()
        {
            Title = "My quiz",
            questions = new List<Question>()
            {
                new Question() {
                    Answers = "1. wrong \r\n 2.correct \r\n 3. wrong",
                    Answer = 2
                },
                new Question() {
                    Answers = "1. wrong \r\n 2.correct \r\n 3. wrong",
                    Answer = 2
                }
            }
        };

        int correct = 0;

        foreach (var question in quiz.questions)
        {
            Console.WriteLine(quiz.Title);
            Console.WriteLine(question.Answers);
            Console.WriteLine("Enter number: ");
            int input = int.Parse(Console.ReadLine());
            if (input == question.Answer) correct++;
        }

        Console.WriteLine("Number of correct answers is " + correct);

    }


    public class Quiz
    {
        public string Title { get; set; }
        public List<Question> questions { get; set; }

    }

    public class Question
    {
        public string Answers { get; set; }
        public int Answer { get; set; }
    }

Comments

0

your system should look something like this

class Question
{
    int Id {get; set;}
    string Text {get; set;}
    Answer[] Answers {get; set;}
}

class Answer
{
    int Id {get; set;}
    int QuestionId {get; set;}
    string Text {get; set;}
    bool IsCorrect {get; set;}
}

class UserAnswer
{
    Question QuestionId {get; set;}
    Timespan TimeSpent {get; set;}
    bool Skipped {get; set;}
    int[] UserSelection {get; set;};
}


static class AnswerEvaluator
{
    static EvalResult Evaluate(UserAnswer ua)
    {
        // your scoring logic goes here
    }
}

class ScoreReport
{
    ScoreReport (UserAnswer[] answers, int passingGrade)
    {
        // . . . . 
    }

    int PercentCorrect {get; set;}
    int QuestionsAnswered {get; set;}
    int QuestionsSkipped {get; set;}
    int TotalScore {get; set;}
    Timespan TotalTime {get; set;}
    bool Passed {get; private set;}


    void EvaluateAll()
    { 
        // . . . .
    }
}

1 Comment

You should not wear your Sequel-Glasses when building data structures ;o)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.