vendredi 29 mai 2020

Im making a quiz using windows forms and C# but it keeps displaying the same question

It reads in questions/answers/topics etc from a .txt file and stores it in an array called ‘questionArray’ which is split by commas into another array called ‘splitDetails’. The if statement selects questions of a particular difficulty level and topic from the list and creates a new question ‘newQ’ of type MultipleChoice which has properties like QuestionText, Choice1, Choice2 etc. and its added to a list called ‘allQuestions’. populateTextboxes() picks a random question from the ‘allQuestions’ list and sets it to the object ‘currentQ’ which is of type MultipleChoice, then deletes it from the list so it isn’t shown again.

{
if (File.Exists("MultipleChoice questions.txt")) //checking file exists
{
   string[] questionArray = File.ReadAllLines("MultipleChoice questions.txt"); //create array of all questions 

   foreach (string Q in questionArray)
   {
        string[] splitDetails = Q.Split(',');  //create new array split by commas
if (splitDetails[6] == Quiz.DifficultyLevel && splitDetails[7] == Quiz.CurrentTopic)
       {
            newQ.QuestionText = splitDetails[0]; 
              newQ.Choice1 = splitDetails[1];
              newQ.Choice2 = splitDetails[2];
              newQ.Choice3 = splitDetails[3];
              newQ.Choice4 = splitDetails[4];
              newQ.Answer = splitDetails[5];
              newQ.Difficulty = splitDetails[6];
              newQ.Topic = splitDetails[7];
              newQ.QuestionType = splitDetails[8]; 

              allQuestions.Add(newQ); 

         }
      }
   }
   Else MessageBox.Show("No question file found", "File not found");
}

//add text to blank question form
public MultipleChoice populateTextboxes() 
{
  Random myNum = new Random();

  if (allQuestions.Count > 0)
  {
    questionCount += 1;

       //select random question from all questions
       int randomNumber = myNum.Next(0, allQuestions.Count);
       currentQ = allQuestions[randomNumber];
       allQuestions.Remove(currentQ);//remove used uestion so it isnt used again
  }

  //when the round is over (3 questions asked)
  if (questionCount == 4) 
  {
    //open keyboardQuestion
    this.Close();
    KeyboardInput k = new KeyboardInput();
    k.Show();

   }

   lblQuestion.Text = currentQ.QuestionText; 
   btnChoice1.Text = currentQ.Choice1;
   btnChoice2.Text = currentQ.Choice2;
   btnChoice3.Text = currentQ.Choice3;
   btnChoice4.Text = currentQ.Choice4;

   return currentQ;
}




Aucun commentaire:

Enregistrer un commentaire