A simple menu in a Console Application

前端 未结 3 693
清歌不尽
清歌不尽 2020-12-09 11:52

I am trying to get my menu to repeat, so after selecting and doing option 1, it will got back to the menu and ask for another option to be chosen

class Progr         


        
3条回答
  •  攒了一身酷
    2020-12-09 12:27

    See: http://msdn.microsoft.com/en-us/library/471w8d85(v=vs.110).aspx

    Replace your main function with:

    static void Main(string[] args) {
        FootballTeams MyCode = new FootballTeams();
    
        MyCode.ListInit();
    
        ConsoleKeyInfo cki;
    
        do {
            MyCode.DisplayMenu();
            cki = Console.ReadKey(false); // show the key as you read it
            switch (cki.KeyChar.ToString()) {
                case "1":
                    MyCode.AddTeams();
                    break;
                case "2":
                    MyCode.DisplayTeams();
                    break;
                // etc..
            }
        } while (cki.Key != ConsoleKey.Escape);
    
    }
    

    Essentially, you need to loop until they press the Escape key. Each time you read the key you can execute the action selected.

提交回复
热议问题