0

Hi I'm developing a simple -multiple choices- quiz program using visual studio. (C#)

I want to make the choices with radio buttons, in which each radio button has a - randomly selected answer- and one of them is the correct one.

I filled an array with the all possible answers. And I want to know how can I make the right answer not in the same place every time?

so how can I make the right answer goes for a random place? and the other places selects other random numbers from the array which are not the right one. :D

I know my question is not that clear I don't know how to explain. ><

4

2 回答 2

1

使用 c#Random()类,并使用Next

Random r = new Random();

choice = possibleChoices[r.Next(possibleChoices.Length-1)];

然后你可以用正确的选择覆盖错误的选择之一

radioButtons[r.Next(radioButtons.Length-1)] = correctAnswer;

文件

于 2012-11-21T17:49:31.720 回答
0

首先,选择您的正确答案,然后随机选择一个索引并将您的正确答案分配给随机单选按钮。

用随机答案填充其他单选按钮。

提示:将单选按钮存储在列表中以方便此操作。您首先用所有单选按钮填充列表,然后在它们填满答案时将它们从列表中删除,这样您就不必处理“我在哪个索引中放入了正确答案”或“复杂按名称手动引用控件的代码”

编辑:正如 Alexei Levenkov 在另一个答案中所指出的,有关如何正确生成随机数的更多信息,请参阅此线程

假设Random random在您的应用程序中声明

List<RadioButton> buttons = new List<RadioButton>();
buttons.Add(answer);
buttons.Add(answer2);
buttons.Add(answer3);
buttons.Add(answer4);

int goodAnswerPos = random.Next(buttons.Count);
buttons[goodAnswerPos].Text = "Good Answer";
buttons.RemoveAt(goodAnswerPos);

foreach (RadioButton button in buttons)
{
    button.Text = "Randomly Selected Wrong Answer";
}

将控件存储在buttons[goodAnswerPos]将使您知道用户在提交答案时是否选择了正确的控件。

于 2012-11-21T17:47:22.203 回答