1
\$\begingroup\$

I've been learning C# for 2 month and in the process of writing a quiz via file i/o. I want to know if there is a better way to getting the questions and answers rather than using switch statement so that it does not look too cluttered. I've tried using fields and properties but I don't know how to implement them into getting the lines in the txt. I've been using switch statement to get the array of the lines and the program work.

Here is a simple of my program:

    public class QuestionUI
    {
       
        String[] fileName = File.ReadAllLines(@"TraviaQuestion.txt");
        private String [] NO_OF_ANSWERS = new string[5];
        private String[] NO_OF_QUESTIONS = new String[5];

 public void Questions(int num)
        {
            NO_OF_QUESTIONS[0] = fileName[0];
   
            switch(num)
            {
                case 1:
                    {
                        Console.WriteLine("\n" + NO_OF_QUESTIONS[0]);
                        GetAnswer(1);
                        break;
                    }
               }
         }

  public void GetAnswer(int num)
        {
            switch (num)
            {
                case 1:
                    {
                        Console.WriteLine(NO_OF_ANSWERS[0] = fileName[1]);
                        Console.WriteLine(NO_OF_ANSWERS[0] = fileName[2]);
                        break;
                    }
            }
         }

 public void GetCorrectAnswer(String input, int num)
        {
            
            switch (num)
            {
                case 1:
                    {
                        if (input.ToUpper() != "B")
                        {
                            Console.WriteLine("Incorrect!");
                            GetExplanation(1);
                        }
                        else
                        {
                            Console.WriteLine("Correct!");
                        }
                        break;
                    }
             }
           }
 public void GetExplanation(int num)
        {
           switch(num)
            {
                case 1:
                    {
                        Console.WriteLine(fileName[6]);
                        break;
                    }
            }
         }
 public void ReadQuestionFile()
        {
            String input;

            Questions(1);
           input = Console.ReadLine();
            GetCorrectAnswer(input,1);
        }

```
\$\endgroup\$
1
  • \$\begingroup\$ Yes there is a better way. This is not C. C# is object oriented. But it's not possible to help you without understanding how your program is supposed to work. How is the flow and what is num? \$\endgroup\$ – BionicCode Dec 2 '20 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.