gpt4 book ai didi

c# - 控制台应用程序中的简单菜单

转载 作者:太空狗 更新时间:2023-10-29 17:27:50 25 4
gpt4 key购买 nike

我试图让我的菜单重复,所以在选择并执行选项 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

最佳答案

您可以为此目的使用 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',代码会在循环中执行。希望对您有所帮助。

关于c# - 控制台应用程序中的简单菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20365214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com