Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9206121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:06:32+00:00 2026-06-18T00:06:32+00:00

Possible Duplicate: Randomize a List<T> in C# I want to make a program that

  • 0

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:06:33+00:00Added an answer on June 18, 2026 at 12:06 am

    You can shuffle indexes of asked question with LINQ

    Random random = new Random();
    var indexes = Enumerable.Range(0,array.Length).OrderBy(i => random.Next());
    
    foreach(var index in indexes)
    {
        Console.WriteLine(array[index]);
        do 
        {        
            string a = Console.ReadLine();
    
            if (a == answer[index]) {                
              Console.WriteLine("It's True\n");
              break;
            }
    
            Console.WriteLine("It's False \n*Please Try Again.");
        }
        while(true);
    }
    

    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:

    public class Question
    {
        public string Text { get; set; }
        public string Answer { get; set; }
    
        public bool IsCorrect(string answer)
        {
            return String.Compare(answer, Answer, true) == 0;
        }
    }
    

    With this question class all your code will be more readable:

    var questions = new List<Question>()
    {
        new Question { Text = "What is the capital of France?", Answer = "Paris" },
        new Question { Text = "What is the capital of Spain?", Answer = "Madrid" },
        new Question { Text = "What is the capital of Russia?", Answer = "Moscow" },
        new Question { Text = "What is the capital of Ukraine?", Answer = "Kiev" },
        new Question { Text = "What is the capital of Egypt?", Answer = "Cairo" }
    };
    
    Random random = new Random();
    
    // randomizing is very simple in this case
    foreach (var question in questions.OrderBy(q => random.Next()))
    {
        Console.WriteLine(question.Text);
    
        do
        {
            var answer = Console.ReadLine();
            if (question.IsCorrect(answer))
            {
                Console.WriteLine("It's True");
                break;
            }
    
            Console.WriteLine("It's False. Please try again.");
        }
        while (true);
    }
    

    Next step for you is implementing Survey class, which will hold question asking, reading answers and displaying summary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Randomize a List<T> in C# shuffle (rearrange randomly) a List<string> Random plot
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How would you pick a uniform random element in linked list with
Possible Duplicate: C#: Is using Random and OrderBy a good shuffle algorithm? I want
Possible Duplicate: Randomize a List<T> in C# i have two lists like below :
Possible Duplicate: Randomize a List<T> in C# I thought I had my code working
Possible Duplicate: Randomize a List<T> in C# I have a list which contains many
Possible Duplicate: Random number generator only generating one random number A beginner question. I
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: How to do the vector of sets in C++? I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.