编程新手 - 需要帮助创建多项选择测验 [英] New to programming - need help creating a multiple choice quiz

查看:61
本文介绍了编程新手 - 需要帮助创建多项选择测验的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用数组进行多项选择测验,我的第一个问题工作正常我只是不知道如何为下一个问题做这个。

我只有一个提交按钮,所以第一个问题的所有代码都连接到该按钮。当你提交第二个答案时,如何使用相同的按钮告诉你' 正确然后继续?



我用c#,到目前为止我的代码是....

I've to make a multiple choice quiz using an array and I've got the first question working fine I just don't know how to do it for the next ones.
I only have one submit button so all the code for the first question is connected to that button. How do I make it so when you submit your second answer, using the same button, it tells you it's correct then moves on?

I use c# and here is my code so far....

int score = 0;
        int i = -1;
        int a = 0;

        string[] questions = new string[] { "What is 9 cubed?", "What is 6+3?", "What type of animal is tuna sandwiches made from?", "What is 18 backwards?" };

        string[] answers = new string[] { "9", "81", "729", "2", "4", "2", "9", "1", "zebra", "aardvark", "fish", "gnu","31","81","91","88"};

private void btnStart_Click(object sender, EventArgs e)
        {
            if (i < questions.Length)
                i++;
            //txtScore.Text = score;

            lblQuestion.Text = questions[i];

            radA.Text = answers[a];
            a++;
            radB.Text = answers[a];
            a++;
            radC.Text = answers[a];
            a++;
            radD.Text = answers[a];
            a++;


            btnStart.Visible = false;
            btnStart.Enabled = false;
            btnSubmit.Visible = true;
            btnSubmit.Enabled = true;
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {

            if(i == 0 && radB.Checked)
            {
                MessageBox.Show("Correct");
                score++;
                txtScore.Text = Convert.ToString(score);
                btnSubmit.Enabled = false;
                btnSubmit.Visible = false;
                btnStart.Visible = true;
                btnStart.Enabled = true;
                btnStart.Text = "Next";
                
            }
            
            else
            {
                MessageBox.Show("Incorrect");
            }

推荐答案

建立在目前为止你所拥有的是一种方式(我强调这只是一个例子,不是最佳实践)



您已经获得了用户所做选择的文本(例如radA.Text),因此有另一个包含正确答案例如

Building on what you have so far here is one way (and I stress this is only an example, not best practise)

You've already got the text of the selection that the user makes (e.g. radA.Text), so have another string array that contains the correct answers e.g.
string[] rightanswers = new string[] { "81", "9", "fish", "81" };



然后在你的 btnStart_Click 事件中确定哪个按钮被选中并将该按钮上的文本与数组中的文本进行比较正确的答案......我用 selectedRadio 来保存Checked单选按钮的文本


Then in your btnStart_Click event work out which button is Checked and compare the text on that button to the array of right answers...I've used selectedRadio to hold the text of the Checked radiobutton

if (selectedRadio == rightanswers[i])
            {
                MessageBox.Show("Correct");





当我三我只是保持它非常简单,有4个if语句......



When I tried this I just kept it very simple and had 4 if statements...

string selectedRadio;
if (radA.Checked) selectedRadio = radA.Text;
if (radB.Checked) selectedRadio = radB.Text;



等(这是安全的)因为一次只能检查一个单选按钮。)


etc (this is safe because only one radio button can be checked at once).


这篇关于编程新手 - 需要帮助创建多项选择测验的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆