Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我怎样才能在测验C中检查正确答案?_C#_Wpf - Fatal编程技术网

C# 我怎样才能在测验C中检查正确答案?

C# 我怎样才能在测验C中检查正确答案?,c#,wpf,C#,Wpf,我一直在尝试制作我的第一个应用程序,一个测验应用程序,但我似乎不知道当点击相应的按钮,然后转到下一个问题时,我将如何验证正确的答案 测验页面: public partial class Page1 : PhoneApplicationPage { // list array List<Question> qu; //counter for moving to next question int questionNumber; // corr

我一直在尝试制作我的第一个应用程序,一个测验应用程序,但我似乎不知道当点击相应的按钮,然后转到下一个问题时,我将如何验证正确的答案

测验页面:

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

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

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

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


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

请发布您的
问题
类。这些是基于意见的问题,不可能有“正确”的答案。我的最佳猜测是,这些是“安全问题”,用户在以前的时间点已经给出了正确的答案。OP可能在使用实际用户输入来确定正确答案之前尝试测试功能。谢谢jon,我现在就开始测试,看看是否可以正常工作。我似乎无法将内容变量传递给endQuestion(),只需更新
endQuestion()
的定义,以获取字符串参数并像这样调用它即可(假设我们在btn\u Answer\u A\u单击
):
endQuestion(btn\u Answer\u A.Content.ToString())`
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;
    }
}