Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading