Search code examples
c#switch-statementconsole.writeline

Attempting to display Console messages in a switch case


For this problem, I am attempting to have Console messages displayed whenever a user inputs a choice between 0-2. But whenever I get to the case, the associated Console messages won't display and neither will the Console message at the end. Adding a default didn't work, and there doesn't seem to be anything else I could do to fix it alone. Any suggestions on what to do and what the problem is?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;

namespace Practice_actual
{
  
    internal class Program
    {  public static void Main(string[] args)
        { 
            Console.WriteLine("Please pick your choice 1-3");
            string answer=Console.ReadLine();
            int choice=Int32.Parse(answer);
            switch(choice)
            {
                    case 0:
                    Console.WriteLine("You chose 1");
                    break;
                    case 1:
                    Console.WriteLine("You chose 2");
                    break ;
                    case 2:
                    Console.WriteLine("You chose 3");
                    break;
                    default:
                    Console.WriteLine("Default option");
                    break;

            }
            Console.WriteLine("we have exited the switch structure");
        }
}

Solution

  • Just add a Console.ReadLine() or Console.ReadKey() at the end to freeze the command window till you enter something