C # вопрос с множественным выбором с radiobutton - PullRequest
0 голосов
/ 06 октября 2019

так что я новичок в программировании, все еще учусь, мне нужно создавать вопросы с множественным выбором, поэтому я создал метод, который выводит вопросы и ответы, но я не знаю, как проверять свои ответы, если нажата кнопка проверки. рад, если некоторые эксперты здесь могут дать мне идею.

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 голосов
/ 06 октября 2019
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 голосов
/ 06 октября 2019
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);
    }
}
...