2013-12-22 3 views
0

텍스트 내용을 텍스트 블록에 표시하는 중입니다. 데이터가 목록에서 검색되고 있습니다. 버튼을 반복해서 누르면 텍스트 블록의 내용이 목록에서 변경됩니다. 내 코드는 목록의 모든 텍스트를 표시하고 모든 텍스트가 표시되었을 때 사용자에게 알립니다.목록에서 텍스트 블록에 제한된 텍스트를 표시하는 방법

예를 들어 목록에 100 개의 항목이있는 경우 사용자 작업에 의해 하나씩 총 100 개의 질문이 표시됩니다. 내가 원하는 건 내 질문 100 개 중 10 개만 채울 수있는 방법이다. 따라서 10 개의 텍스트가 표시된 후에 완성 된 메시지가 표시됩니다. 나는 또한 각 텍스트가 바뀔 때 목록을 무작위로 추출하고 섞어 놓고 싶다. 그래서 사용자는 페이지를 열 때마다 (적어도 얼마 동안) 다른 것들을 만날 것입니다.

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); 
     } 
    } 
} 

위의 목록은 20 텍스트를 포함 할 수 있습니다 이 내 코드입니다. 100 개 또는 그 이상으로 만들고 싶습니다. 그러나 나는 모든 것을 보여줄 수 있습니다. 나는 어떻게 10을 말로 표현할 것인가. 따라서 다른 텍스트 세션을 시작하려면 페이지를 다시 열어야합니다.

+0

와 수정을 표시하려면? – Tony

+0

Windows 전화 앱입니다. 실버 라이트. wpf를 사용하지 않습니다. –

답변

0

처음에는 질문 목록의 모든 항목으로 채워진 다른 임시 목록을 사용할 수 있습니다.

다음에 사용자가 다음을 누를 때마다 임시 목록에서 이전 질문을 제거하고 currentQuestionIndex에 새로운 임의 색인을 지정하십시오.

임시 목록이 비어 있으면 사용자가 모든 질문을 완료했습니다.

 //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); 

메시지 각 10 개 가지 질문

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

그리고이 코드는 WPF를 사용하고 이러한 변경

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)); 
     } 
    } 
} 
0

카운트 10, 및 랜덤 지수 :

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++; 
} 

하면이를 방지 할 수있는 방법을 파악 할 수 있도록이 같은 인덱스를 여러 번 반환 할 수 있음을 염두에두고. 한 가지 방법은 사용 된 색인의 간단한 목록을 유지하는 것입니다.

관련 문제