문제

Am displaying text contents into a textblock. the data is being retrieved from a list. Pressing a button repeatedly changes the content of the textblock from the list. My code displays all the texts in the list and notifies the user when all the texts have been displayed.

For example, if i have 100 items in the list, a total of 100 questions will appear one by one by the user's action. What i want is how i can set it to populate just 10 out of 100 of my questions. So a completed message will show after 10 texts is displayed. I also want to randomize and shuffle the list at each text changing. so the user will meet different things each time opening the page( at least for some time). This is my code:

public partial class _008Test : PhoneApplicationPage
{
    private List<Question> questionList;
    int currentQuestionIndex = 0;
    private Question currentQuestion;
    int Score = 0;
    public _008Test()
    {
        InitializeComponent();
        InitializeComponent();
        questionList = new List<Question>();
        questionList.Add(new Question { Text = "This is the first question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the second question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the third question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the forth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the fifth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the sixth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the seventh question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the eitht question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the ninth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the tenth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
    questionList.Add(new Question { Text = "This is the first question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the second question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the third question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the forth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the fifth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the sixth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the seventh question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the eitht question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the ninth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the tenth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });

        loadquestion(currentQuestionIndex);
    }
    private void loadquestion(int questionindex)
    {
        currentQuestion = questionList[questionindex];
    }

    private void Next_Click(object sender, System.EventArgs e)
    {
        Score++;
        currentQuestionIndex++;
        if (currentQuestionIndex < questionList.Count)
        {
            loadquestion(currentQuestionIndex);
        }
        else
        {
            MessageBox.Show("You have finished!" + "Score is: " + Score);
        }
    }
}

The list above can contain 20 texts. I want to make it 100 or even more. But i can only display everything. How will i display say 10 only. So the page has to be reopened in order to initiate another text session.

도움이 되었습니까?

해결책

You can use another temp list initially populated with all items in your questions list.

Then each time user press next, remove the previous question from temp list and assign currentQuestionIndex a new random index.

if the temp list is empty then user have completed all your questions set.

        //if(CorrectAnswer)
          Score++;
        questionNumber++;

        if (currentQuestionIndex < tempList.Count)
        {
            //delete previous item from tempList
            tempList.RemoveAt(currentQuestionIndex);
        }

        //if no more questions, then Display completed and disable Next Button
        if (tempList.Count == 0)
        {
            MessageBox.Show(String.Format("Your have completed , Your Final score is {0}", Score));
            return;
        }

        Random rand = new Random();
        //assign currentQuestionIndex a new random number
        currentQuestionIndex = rand.Next(0, tempList.Count);
        loadquestion(currentQuestionIndex);

To display a message each ten questions

questionNumber++;    
if (questionNumber % 10 == 0)
  {
   //show completed messages each time at 10,20,30,.....
  }

And this is your code modified with these changes

 public partial class Page1 : PhoneApplicationPage
{
    private List<Question> questionList;
    private int currentQuestionIndex = 0;
    private Question currentQuestion;
    private int Score = 0;
    public Page1()
    {
        InitializeComponent();
        questionList = new List<Question>();
        questionList.Add(new Question { Text = "This is the first question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the second question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the third question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the forth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the fifth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the sixth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the seventh question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the eitht question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the ninth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the tenth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the first question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the second question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the third question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the forth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the fifth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the sixth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the seventh question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the eitht question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        questionList.Add(new Question { Text = "This is the ninth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 1 });
        questionList.Add(new Question { Text = "This is the tenth question", Answers = new List<string> { "A", "B", "C", "D" }, CorrectAnswer = 2 });
        tempList = new List<Question>(questionList);

        Random rand = new Random();
        //assign currentQuestionIndex new random number
        currentQuestionIndex = rand.Next(0, tempList.Count);
        loadquestion(currentQuestionIndex);

    }
    private void loadquestion(int questionindex)
    {
        currentQuestion = tempList[questionindex];
    }

    private List<Question> tempList;
    private int questionNumber = 0;
    private void Next_Click(object sender, System.EventArgs e)
    {
        //if(CorrectAnswer)
        Score++;
        questionNumber++;
        if (currentQuestionIndex < tempList.Count)
        {
            //delete previous item from tempList
            tempList.RemoveAt(currentQuestionIndex);
        }

        //if no more questions, then Display completed and disable Next Button
        if (tempList.Count == 0)
        {
            MessageBox.Show(String.Format("Your have completed , Your Final score is {0}", Score));
            return;
        }
        Random rand = new Random();
        //assign currentQuestionIndex a new random number
        currentQuestionIndex = rand.Next(0, tempList.Count);
        loadquestion(currentQuestionIndex);

        if (questionNumber % 10 == 0)
        {
            //show level messages each time at 10,20,.......
            MessageBox.Show(String.Format("Your score is {0}", Score));
        }
    }
}

다른 팁

Count to 10, and randomize index:

int questionNum = 0;
Random rand = new Random();
int numOfQuestionsInTheGame = 100; // this is the number of all your questions
private void Next_Click(object sender, System.EventArgs e)
{
    Score++;
    if (questionNum < 10)
    {
        loadquestion(rand.Next(0, numOfQuestionsInTheGame));
    }
    else
    {
        MessageBox.Show("You have finished!" + "Score is: " + Score);
    }
    questionNum++;
}

Have in mind that this can return the same index multiple times, so you might want to figure a way to avoid that. One way might be to keep a simple list of used indexes.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top