控制台应用程序中的简单菜单 [英] A simple menu in a Console Application
问题描述
我正在尝试重复菜单,因此在选择并执行选项1之后,它将返回菜单并要求选择另一个选项
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() + " ");
}
我知道我的措辞很差,所以我希望有人能理解我的意思'm ask:P
I know I have worded this poorly, so I hope that someone understands what I'm asking :P
推荐答案
您可以使用do while循环来实现
的目的。 DispalyMenu()方法并返回类似结果
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);
}
并将其写入Main()方法
and write this in your Main() method
int userInput = 0;
do
{
userInput = DisplayMenu();
}while(userInput!=5);
因此,当用户暂时未输入 5时,代码将在循环中执行。
希望有帮助。
So for the time being the user does not enter '5', the code will execute in the loop. Hope that helps.
这篇关于控制台应用程序中的简单菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!