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

443
Views
¿Cómo refactorizar este interruptor para eliminar GOTO?

Soy nuevo en C# y estoy creando un juego basado en texto. Al terminarlo, aprendí que los gotos son engendros de Satanás (pffft). Tengo muchas instancias en esta secuencia de comandos en las que le hago al jugador una pregunta de sí o no, y si el jugador ingresa algo más, recibe un mensaje de error y vuelvo al inicio del interruptor usando un goto y una etiqueta. Entonces, dado el ejemplo a continuación, ¿cuál sería una forma de volver a trabajar este código para producir los mismos resultados sin usar un goto? ¡Gracias!

 public static void One() { string response; Console.BackgroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Yellow; Console.Clear(); Console.WriteLine("Some text."); OneOne: Console.ReadKey(); Console.Write("\nA question?"); response = Console.ReadLine().ToLower(); Console.Clear(); switch (YesOrNoDetector.Detect(response)) { case YesOrNo.Yes: { Console.WriteLine("A response!"); Console.ReadKey(); Two(); break; } case YesOrNo.No: { Console.WriteLine("A different response."); Console.ReadKey(); Three(); break; } case YesOrNo.Undefined: { Console.WriteLine("Refresh switch to look for a yes/no answer"); goto OneOne; } } }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

//OneOne: var haveValidAnswer = false; while (! haveValidAnswer) { Console.ReadKey(); ... // as before, but with control of haveValidAnswer }

Debería aprender a diseñar el flujo de control sin goto s. Un problema es que la lógica goto suele ser fácil de escribir pero mucho más difícil de leer y mantener.

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!