Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

c# - A simple menu in a Console Application

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 Program
{
    static void Main(string[] args)
    {

        FootballTeams MyCode = new FootballTeams();

        MyCode.ListInit();
        MyCode.DisplayMenu();
        MyCode.AddTeams();
        Console.ReadLine();

        MyCode.ListInit();
        MyCode.DisplayMenu();
        MyCode.DisplayTeams();
        Console.ReadLine();

        MyCode.ListInit();
        MyCode.DisplayMenu();
        MyCode.Delete();
        Console.ReadLine();

        MyCode.ListInit();
        MyCode.DisplayMenu();
        MyCode.TeamSearch();
        Console.ReadLine();
    }
}

Here are the methods with the contents taken out:

class FootballTeams
{


    public FootballTeams(){ }

    List<string> teams;
    public void ListInit()


    public void DisplayMenu()
   {   
    Console.WriteLine("Football Manager");
    Console.WriteLine();
    Console.WriteLine("1. Add a Football team");
    Console.WriteLine("2. List the Football teams");
    Console.WriteLine("3. Search for a Football team");
    Console.WriteLine("4. Delete a team");
    Console.ReadLine();
    }



    public void AddTeams()
    {
      Console.WriteLine("Enter a team to be added: ");
      string userinput = Console.ReadLine();
      if (teams.Count < 10)
      {
       if (userinput != "Colchester")
        {
          teams.Add(userinput);
          foreach (var item in teams)
          Console.Write(item.ToString() + " ");
         }
        else
          Console.Write("NOT ALLOWED");
         }
       else
         Console.Write("MAXIMUM LIMIT REACHED");
      }


    public void DisplayTeams()
    {
     foreach(var item in teams)
     Console.Write(item.ToString() + " ");
    }

    public void TeamSearch()
    {
     Console.WriteLine("Please enter the team you wish to search for: ");
     string userinput = Console.ReadLine();
     if (teams.Contains(userinput))
     Console.WriteLine("Success, team " + userinput);
    }

    public void Delete()
    {
      Console.WriteLine("Enter a team you wish to delete: ");
      string userinput = Console.ReadLine();
      teams.Remove(userinput);
      foreach (var item in teams)
      Console.Write(item.ToString() + " ");
    }

I know I have worded this poorly, so I hope that someone understands what I'm asking :P

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use a do while loop for the purpose Make a little modification to your DispalyMenu() method and return the result like this

static public int DisplayMenu()
{   
  Console.WriteLine("Football Manager");
  Console.WriteLine();
  Console.WriteLine("1. Add a Football team");
  Console.WriteLine("2. List the Football teams");
  Console.WriteLine("3. Search for a Football team");
  Console.WriteLine("4. Delete a team");
  Console.WriteLine("5. Exit");
  var result = Console.ReadLine();
  return Convert.ToInt32(result);
}

and write this in your Main() method

int userInput = 0;
do
{
  userInput = DisplayMenu();
}while(userInput!=5);

So for the time being the user does not enter '5', the code will execute in the loop. Hope that helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...