0

我正在使用 C# 开发一个窗口电话应用程序。我有 2 个文本块和四个单选按钮。第一个文本块是显示问题,而第二个是显示递增变量作为结果。通过加载页面,它会在文本块中显示第一个问题。通过选中单选按钮并单击应用程序栏,程序应验证选中的单选按钮布尔值,如果选中右侧单选按钮,则向变量 a 添加增量,然后将 textBlock1.text 更改为另一个文本(第二个问题) . 然后另一个单选按钮检查和应用程序栏单击应重复上述步骤。然后经过三个问题,变量a的最终值显示在第二个textBlock上。为了填充问题,我使用了这个数组,但它没有用:

public void Click_AppBar(object sender, System.EventArgs e)
        {
            // TODO: Add event handler implementation here.

        String[] pages = 
        {"welcome to question 2", 
            "welcome to question 3",
            "welcome to question 4"};
        foreach (string s in pages)
            textBlock1.Text = (s);
    }`

它没有用。它只在文本块中显示“欢迎来到问题 4”,但我尝试了从这个网站获得的这段代码,它有效:

 `private List<string> questions= new List<string>()
        {"welcome to question 2", "welcome to question 3", "welcome to question 4"};
        private int clickCount = 0;

        protected void Click_AppBar(object sender, EventArgs e)
        {
        textBlock1.Text = questions[clickCount];
        clickCount++;
        if (clickCount == questions.Count)
        clickCount = 0;`

该数组成功显示了每次点击的问题,但只要单击,它就会不断重复显示问题 2、3、4 的变化。另外,我不知道如何添加代码来验证布尔检查并在选中右侧单选按钮时增加变量。
这是我现在使用的代码

`public void Click_AppBar(object sender, System.EventArgs e)
    {
        // TODO: Add event handler implementation here.

        int a= 0;
            if (RadioA.IsChecked == true)
            {

                a++;

            }

            textBlock1.Text = ("welcome to question 2");

            else if (RadioB.IsChecked == true)
            {

                a++;

            }

            textBlock1.Text = ("welcome to question 3");
            else if (RadioC.IsChecked == true)
            {

                a++;

            }

            ShowResult.Text = (a.ToString());
            MessageBox.Show("You have finished");
        }

    }`

我寻求1)增量,2)单选按钮布尔验证和豁免管理的帮助。我学习 C# 才一年。请不要介意我的代码,因为我还在学习。谢谢

4

1 回答 1

0

我想这就是你想要的:

private List<Questions> questions = new List<Questions>()
    {
        new Questions()
        {
             Question = "welcome to question 1",
             CorrectAnswer = 2,
        },
        new Questions()
        {
             Question = "welcome to question 2",
             CorrectAnswer = 1,
        },
        new Questions()
        {
            Question =  "welcome to question 3",
            CorrectAnswer = 3,
        },
        new Questions()
        {
            Question = "welcome to question 4",
            CorrectAnswer = 2,
        },

    };
    int currentQuestionIndex = 0;
    private int numberCorectAnswer = 0;

    public List<RadioButton> listRadioButton = null;

    public void Click_AppBar(object sender, System.EventArgs e)
    {
        if (listRadioButton == null)
        {
            listRadioButton = new List<RadioButton>()
            {
                RadioA,
                RadioB,
                RadioC,
            };
        }
        Questions previousQuestions = this.questions[currentQuestionIndex];

        if (listRadioButton[previousQuestions.CorrectAnswer].IsChecked == true)
        {

            numberCorectAnswer++;
        }


        if (currentQuestionIndex == questions.Count-1)
        {
            MessageBox.Show("You have finished, number correct answer:" +numberCorectAnswer);
        }

        currentQuestionIndex++;
        textBlock1.Text = this.questions[currentQuestionIndex];

        //ShowResult.Text = (a.ToString());

    }
于 2013-09-11T13:24:12.917 回答