0

我一直在尝试制作我的第一个应用程序,一个测验应用程序,但我似乎无法理解当点击相应的按钮然后继续下一个问题时我将如何验证正确答案。

测验页面:

public partial class Page1 : PhoneApplicationPage
{
    // list array
    List<Question> qu;

    //counter for moving to next question
    int questionNumber;

    // correct answer counter
    int correctAnswer;


    public void main()
    {
        // method for adding more questions to the list
        qu = new List<Question>();
        qu.Add(new Question("what is your favourite colour?", 
                            "blue", 
                            "red", 
                            "green", 
                            "blue"));

        qu.Add(new Question("what is your favourite film?", 
                            "The Matrix", 
                            "Star Wars", 
                            "Wrath Of Khan", 
                            "The Matrix"));

        qu.Add(new Question("what is your favourite car?", 
                            "BMW", 
                            "VW", 
                            "Mercedes", 
                            "BMW"));
        questionNumber = 0;
        correctAnswer = 0;

    }

    public Page1()
    {
        InitializeComponent();
        main();
        // counter for displaying next question
        displayQuestion(questionNumber);


    }

    // button for quitting back to the start screen
    private void btn_quit_Click(object sender, RoutedEventArgs e)
    {

        NavigationService.Navigate(
           new Uri("/MainPage.xaml", UriKind.Relative));
    }

    private void btn_Answer_A_Click(object sender, RoutedEventArgs e)
    {
        endQuestion();
    }

    private void btn_Answer_C_Click(object sender, RoutedEventArgs e)
    {
        endQuestion();
    }

    private void btn_Answer_B_Click(object sender, RoutedEventArgs e)
    {
        endQuestion();
    }

    // method for ending a question, inc : counter for 
    // moving on to next question and
    // checking if the answer is correct or not
    public void endQuestion()
    {
        //check the answer
            //if it's correct
                questionNumber++;
                displayQuestion(questionNumber);
            //otherwise 
                //stay on question
    }

    public void displayQuestion(int counter)
    {
        // where the question and answers are displayed 
        // in the buttons and txt block
        txt_block_question.Text = counter + ". " + qu[counter].question;
        btn_Answer_A.Content = qu[counter].a;
        btn_Answer_B.Content = qu[counter].b;
        btn_Answer_C.Content = qu[counter].c;
    }
}

问题类

public class Question
{

    public String question;
    public String answer;
    public String a;
    public String b;
    public String c;


    public Question(string q, string an, string optionA, string optionB, string optionC)
    {
        question = q;
        answer = an;
        a = optionA;
        b = optionB;
        c = optionC;
    }
}
4

1 回答 1

1

根据您显示的代码,我认为解决方案相当简单:

首先,您需要将一个变量传递给endQuestion(),告诉它选择了哪个答案。由于每个答案按钮都将答案文本存储在其Content值中,因此您可以传递它。

其次,您需要更新endQuestion()以获取“答案”参数,并将该答案与存储在Question变量 ( qu[counter].answer) 中的正确答案进行比较。使用String.Compare(string, string)将是执行比较的好方法。

这应该是您开始所需的全部内容。我将把实际的代码实现留给你。

于 2013-04-23T15:11:45.507 回答