Possible Duplicate:
Randomize a List<T> in C#
I want to make a program that ask the user 5 questions in random without duplicate the questions, and if the question is right go through the next question if wrong stop until he gave the right answer, and this is the code which I had written, but there are still some problems, like there are duplicate and when the user enter the wrong answer it only stop for one time, and then the program closed!
now how can I prevent duplicate the same question, and if he enter wrong value don’t proceed to the next question or the program closed?
static void Main()
{
next:
Random question = new Random();
int x = question.Next(5);
string[] array = new string[5];
array[0] = "-What is the capital of France";
array[1] = "-What is the capital of Spain";
array[2] = "-What is the captial of Russia";
array[3] = "-What is the capital of Ukraine";
array[4] = "-What is the capital of Egypt";
Console.WriteLine(array[x]);
string[] answer = new string[5];
answer[0] = "Paris";
answer[1] = "Madrid";
answer[2] = "Moscow";
answer[3] = "Kiev";
answer[4] = "Cairo";
string a = Console.ReadLine();
if (a == answer[x])
{
Console.WriteLine("It's True \n*Next Question is:");
goto next;
}
else
Console.WriteLine("It's False \n*Please Try Again.");
Console.ReadLine();
}
You can shuffle indexes of asked question with LINQ
Explanation:
Enumerable.Range(0,array.Length)will return range of integer values starting from zero:0, 1, 2, 3, 4. Next those numbers will be sorted by random number (i.e. shuffled). It could result in any combination of those numbers, e.g.3, 0, 1, 4, 2.BTW it’s good to go OOP way and put related data (question text and answer) and logic (defining if answer is correct) into one place:
With this question class all your code will be more readable:
Next step for you is implementing
Surveyclass, which will hold question asking, reading answers and displaying summary.