我正在尝试重复我的菜单,所以在选择并执行选项1后,它将返回菜单并要求选择其他选项
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();
}
}
以下是取出内容的方法:
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() + " ");
}
我知道我的措辞很差,所以我希望有人理解我的要求:P
答案 0 :(得分:18)
您可以使用do while循环 对你的DispalyMenu()方法做一点修改,然后像这样返回结果
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()方法
int userInput = 0;
do
{
userInput = DisplayMenu();
}while(userInput!=5);
因此,暂时用户不输入'5',代码将在循环中执行。 希望有所帮助。
答案 1 :(得分:12)
请参阅:http://msdn.microsoft.com/en-us/library/471w8d85(v=vs.110).aspx
将主要功能替换为:
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);
}
基本上,你需要循环直到他们按Esc键。每次阅读密钥时,您都可以执行所选的操作。
答案 2 :(得分:6)
现在有一个nuget包
https://github.com/splttingatms/EasyConsole
实施例
添加nuget包之后,菜单可以通过以下方式实现,这是一个非常基本的选项
static void Main(string[] args)
{
var menu = new EasyConsole.Menu()
.Add("foo", () => Console.WriteLine("foo selected"))
.Add("bar", () => Console.WriteLine("bar selected"));
menu.Display();
}
在操作中,您可以放置任何方法以在选中时运行
这将输出类似这样的内容
选择一个选项: