Hey,

I'm trying to work on a project that asks a question and tyhen give three possible answers.

The problem is that I want the answers to be in random places.. So like the answer could be in in the second place 1 time and at the top again.

I've tried this:

possibleAnswers[0] = Convert.ToInt32(s.getArea());
possibleAnswers[1] = 110;
possibleAnswers[2] = 20;

possible1.Text = Convert.ToString(possibleAnswers[RandNum.Next(0, 3)]);
possible2.Text = Convert.ToString(possibleAnswers[RandNum.Next(0, 3)]);
possible3.Text = Convert.ToString(possibleAnswers[RandNum.Next(0, 3)]);

But it doesn't work, help me? :(

I think the problem is that you're not checking if you generate the same number twice of three times before you use them.

I added/changed this to your code

int first, second, third;

first = RandNum.Next(0, 3);

do{
    second = RandNum.Next(0, 3);
} while (second == first);

do{
    third = RandNum.Next(0, 3);
} while ((third == first) || (third == second));

possible1.Text = Convert.ToString(possibleAnswers[first]);
possible2.Text = Convert.ToString(possibleAnswers[second]);
possible3.Text = Convert.ToString(possibleAnswers[third]);

This part stays the same:

possibleAnswers[0] = Convert.ToInt32(s.getArea());
possibleAnswers[1] = 110;
possibleAnswers[2] = 20;

Here you go:

Random r = new Random();
        public Form1()
        {
            InitializeComponent();
            label1.Text = "Which is the 3rd planet from the sun?";
            DeselectAll();
        }

        private void DeselectAll()
        {
            RadioButton[] rbs = { radioButton1, radioButton2, radioButton3 };
            foreach (RadioButton rb in rbs)
            {
                rb.Checked = false;
                rb.Text = "";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DeselectAll();
            string[] answers = new string[] { "Mars", "Jupiter", "Earth" };
            string[] randomAnswers = answers.OrderBy(o => r.Next()).ToArray();
            radioButton1.Text = randomAnswers[0];
            radioButton2.Text = randomAnswers[1];
            radioButton3.Text = randomAnswers[2];
        }

Hey,

It works, but, when a question loads it goes through the loop really fast? :S and when you click on an answer, it goes through the loop again?

Thanks for your answer btw :)

I did a whole example with one question and 3 answers.
Take a look at here:

Random r = new Random();
        string correctAnswer;
        public Form1()
        {
            InitializeComponent();
            label1.Text = "";
            RadioButton[] rbs = { radioButton1, radioButton2, radioButton3 };
            foreach (RadioButton rb in rbs)
                rb.CheckedChanged += new EventHandler(rb_CheckedChanged);
            DeselectAll(false);
        }

        private void DeselectAll(bool bShow)
        {
            RadioButton[] rbs = { radioButton1, radioButton2, radioButton3 };
            foreach (RadioButton rb in rbs)
            {
                if (bShow)
                    rb.Visible = true;
                else
                    rb.Visible = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DeselectAll(true);

            //this code bellow is only an example of question and answers. 
            //you need to do your own code of question
            
            label1.Text = "Which is the 3rd planet from the sun?";
            string[] answers = new string[] { "Mars", "Jupiter", "Earth" };
            correctAnswer = "Earth";
            string[] randomAnswers = answers.OrderBy(o => r.Next()).ToArray();
            radioButton1.Text = randomAnswers[0];
            radioButton2.Text = randomAnswers[1];
            radioButton3.Text = randomAnswers[2];
        }

        private void rb_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            if (rb.Checked)
            {
                string selectedAnswer = rb.Text;
                if (selectedAnswer.Equals(correctAnswer))
                    MessageBox.Show("Answer is correct.");
                else
                    MessageBox.Show("Answer is wrong!");
            }
        }

Hey,

It's because I'm using a panel that seems to be refreshing every-time I hover over it!

Thanks :)

Hey,

It works, but, when a question loads it goes through the loop really fast? :S and when you click on an answer, it goes through the loop again?

Thanks for your answer btw :)

You shouldn't notice the loops, they only have to check 2 integers tops.

And you're welcome :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.