Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
Incorrect behavior of switch statement in while loop in C#

I have a problem with my code. Mostly I have no problems with the switch statement combined with the do-while loop. I think the problem lies in the ConsoleKey function, but since I start my adventure with programming, I can't solve the problem. So I hope someone from this great community will help me. If it's possible I want to use press key to choose an option in switch statement. Thanks for help. P.S. English is not my first language, so if I misspelled anything, sorry.

static void Main(string[] args)
        {
            Console.WriteLine("Attention");
            Console.WriteLine("Choose 1-2");
            var inputData = Console.ReadKey();
            do
            {

               switch (inputData.Key)
                {
                    case ConsoleKey.X:
                        Console.WriteLine("action 1");
                        break;
                    case ConsoleKey.Y:
                        Console.WriteLine("action 2");
                        break;
                    default:
                        Console.WriteLine("Default");
                        break;
                }
            } while (inputData.Key != ConsoleKey.Escape);
            Console.ReadKey();
        }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

            Console.WriteLine("Attention");
            Console.WriteLine("Choose 1-2");
            ConsoleKeyInfo inputData = Console.ReadKey();
            do
            {

                switch (inputData.Key)
                {
                    case ConsoleKey.X:
                        Console.WriteLine("action 1");
                        break;
                    case ConsoleKey.Y:
                        Console.WriteLine("action 2");
                        break;
                    default:
                        Console.WriteLine("Default");
                        break;
                }

                Console.WriteLine("Choose 1-2");
                inputData = Console.ReadKey();
            } while (inputData.Key != ConsoleKey.Escape);

            Console.ReadKey();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!