首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带单选按钮的C#多选题

带单选按钮的C#多选题
EN

Stack Overflow用户
提问于 2019-10-06 14:23:35
回答 2查看 403关注 0票数 0

所以我是编程新手,还在学习创建多项选择题,所以我做了一个方法,输出问题和答案,但我不知道如何验证我的答案,如果按钮检查为clicked.will,如果这里的一些专家能给我一个想法,我会很高兴。

代码语言:javascript
复制
public partial class Form1: Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void PresentQuestion()
    {
        radioButton1.Checked = false;
        radioButton2.Checked = false;
        radioButton3.Checked = false;
        radioButton4.Checked = false;
        Random rand = new Random();
        int cycles = rand.Next(1, 4);

        switch (cycles)
        {
            case 1:
                textBox1.Text = "what is 15 - 8 ?";


                radioButton1.Text = "A. 6";
                radioButton2.Text = "B. 7";
                radioButton3.Text = "C. 5";
                radioButton4.Text = "D. 4";

                break;

            case 2:
                textBox1.Text = "what is 5 x 5?";

                radioButton1.Text = "A. 25";
                radioButton2.Text = "B. 50";
                radioButton3.Text = "C. 20";
                radioButton4.Text = "D. 100";


                break;

            case 3:
                textBox1.Text = "what is 4+4?";

                radioButton1.Text = "A. 4";
                radioButton2.Text = "B. 2";
                radioButton3.Text = "C. 2";
                radioButton4.Text = "D. 8";

                break;
        }
    }


    private void Form1_Load(object sender, EventArgs e)
    {
        PresentQuestion();
    }

    private void buttonCheck_Click(object sender, EventArgs e)
    {
        PresentQuestion();


    }
}

}

EN

回答 2

Stack Overflow用户

发布于 2019-10-06 14:44:03

代码语言:javascript
复制
Try this ...

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    int cycles = 0;

    private void PresentQuestion(int cycles)
    {
        switch (cycles)
        {
            case 1:
                textBox1.Text = "what is 15 - 8 ?";

                radioButton1.Text = "A. 6";
                radioButton2.Text = "B. 7";
                radioButton3.Text = "C. 5";
                radioButton4.Text = "D. 4";

                break;

            case 2:
                textBox1.Text = "what is 5 x 5?";

                radioButton1.Text = "A. 25";
                radioButton2.Text = "B. 50";
                radioButton3.Text = "C. 20";
                radioButton4.Text = "D. 100";


                break;

            case 3:
                textBox1.Text = "what is 2+2?";

                radioButton1.Text = "A. 4";
                radioButton2.Text = "B. 2";
                radioButton3.Text = "C. 2";
                radioButton4.Text = "D. 8";

                break;
        }
    }

    private void Button1_Click(object sender, EventArgs e)
    {
        switch (cycles)
        {
            case 1:
                if(radioButton2.Checked)
                {
                    MessageBox.Show("correct");
                }
                else
                {
                    MessageBox.Show("wrong");
                }
                break;

            case 2:
                if (radioButton1.Checked)
                {
                    MessageBox.Show("correct");
                }
                else
                {
                    MessageBox.Show("wrong");
                }
                break;

            case 3:
                if (radioButton1.Checked)
                {
                    MessageBox.Show("correct");
                }
                else
                {
                    MessageBox.Show("wrong");
                }
                break;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        radioButton1.Checked = false;
        radioButton2.Checked = false;
        radioButton3.Checked = false;
        radioButton4.Checked = false;

        Random rand = new Random();
        cycles = rand.Next(1, 4);

        PresentQuestion(cycles);
    }
}
票数 0
EN

Stack Overflow用户

发布于 2019-10-06 14:45:03

代码语言:javascript
复制
public partial class Form1 : Form
{
    int total = 0;
    public Form1()
    {
        InitializeComponent();
    }
    private void PresentQuestion()
    {
        radioButton1.Checked = false;
        radioButton2.Checked = false;
        radioButton3.Checked = false;
        radioButton4.Checked = false;
        Random rand = new Random();
        int cycles = rand.Next(1, 4);

        switch (cycles)
        {
            case 1:
                textBox1.Text = "what is 15 - 8 ?";
                total = 15 - 8;

                radioButton1.Text = "6";
                radioButton2.Text = "7";
                radioButton3.Text = "5";
                radioButton4.Text = "4";

                break;

            case 2:
                textBox1.Text = "what is 5 x 5?";
                total = 5 * 5;
                radioButton1.Text = "25";
                radioButton2.Text = "50";
                radioButton3.Text = "20";
                radioButton4.Text = "100";


                break;

            case 3:
                textBox1.Text = "what is 2+2?";
                total = 2 + 2;
                radioButton1.Text = "4";
                radioButton2.Text = "2";
                radioButton3.Text = "2";
                radioButton4.Text = "8";

                break;
        }
    }

    private bool CheckOFResult()
    {
        foreach(Control r in this.Controls)
        {
            if(r.GetType() == typeof(RadioButton))
            {
                RadioButton radio = (RadioButton)r;
                if (radio.Checked)
                {
                    if (int.Parse(radio.Text) == total)
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        PresentQuestion();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (CheckOFResult())
        {
            MessageBox.Show("Correct");
        }else
        {
            MessageBox.Show("Incorrect");
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        PresentQuestion();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58254753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档