Answers for "c# main menu console"

C#
1

c# console menu system

class Program
{
	static void Main(string[] args)
	{
    	bool showMenu = true;
        while (showMenu)
		{
        	showMenu = MainMenu();
		}
	}
    
	private static bool MainMenu()
    {
    	Console.Clear();
        Console.WriteLine("Choose an option:");
        Console.WriteLine("1) Reverse String");
        Console.WriteLine("2) Remove Whitespace");
        Console.WriteLine("3) Exit");
		Console.Write("rnSelect an option: ");
 
        switch (Console.ReadLine())
        {
        	case "1":
				ReverseString();
                return true;
			case "2":
            	RemoveWhitespace();
                return true;
			case "3":
            	return false;
			default:
            	return true;
		}
	}
 
	private static string CaptureInput()
    {
    	Console.Write("Enter the string you want to modify: ");
        return Console.ReadLine();
	}
 
    private static void ReverseString()
    {
    	Console.Clear();
        Console.WriteLine("Reverse String");
 
        char[] charArray = CaptureInput().ToCharArray();
        Array.Reverse(charArray);
        DisplayResult(String.Concat(charArray));
	}
 
    private static void RemoveWhitespace()
    {
    	Console.Clear();
        Console.WriteLine("Remove Whitespace");
 
		DisplayResult(CaptureInput().Replace(" ", ""));
	}
 
    private static void DisplayResult(string message)
    {
    	Console.WriteLine($"rnYour modified string is: {message}");
        Console.Write("rnPress Enter to return to Main Menu");
        Console.ReadLine();
	}
}
Posted by: Guest on July-24-2021

C# Answers by Framework

Browse Popular Code Answers by Language