带单选按钮的C#选择题

时间:2019-10-06 06:23:35

标签: c# validation radio-button user-input

所以我还是编程新手,我仍然必须创建多项选择题,所以我制作了一种输出问题和答案的方法,但是如果单击按钮检查,我不知道如何验证答案。很高兴这里的一些专家能给我一个主意。

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


    }
}

}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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