Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

110
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda