如何使抓取列表动态化?

时间:2019-07-23 16:03:32

标签: c#

所以我想做的是使我的代码动态化。我的if语句非常有用,因为它在readline上一次遍历列表一次。但是我的问题是我不确定如何使它动态化。我不想为我创建的每个列表重复if语句。如何重新排列/设置它,以便程序将readline输入与任何列表进行比较,如果它与列表匹配,则在foreach循环和writeline区域中使用该列表?

我只是尝试重复自己,这不是一个好习惯,而且我找不到一种方法来列出可以与readline输入进行比较的列表。

Start:
    List<string> food = new List<string> { "banana", "apple", "steak", "chicken" };
    List<string> languages = new List<string> { "english", "greek", "spanish", "russian" };
    List<string> tech = new List<string> { "facebook", "apple", "google", "samsung" };
    List<string> colors = new List<string> { "blue", "green", "yellow", "red" };

    Console.WriteLine("Please choose a category: food, languages, tech, or colors.");
    string input = Console.ReadLine();
    if (input == "food")
    {
        foreach (string foods in food)
        {
            Console.ReadLine();
            Console.WriteLine(foods);
        }
    }

    goto Start;

我期望的是应用程序应该像已经运行的那样运行,但是IF语句应该动态地抓住与readline输入匹配的任何列表,并相应地调整该语句以遍历该列表。

4 个答案:

答案 0 :(得分:2)

您不能动态引用变量名。如何将列表存储在键控字典中呢?

List food = new List { "banana", "apple", "steak", "chicken" }; 
List languages = new List { "english", "greek", "spanish", "russian" }; 
List tech = new List { "facebook", "apple", "google", "samsung" }; 
List colors = new List { "blue", "green", "yellow", "red" };

var lists = new Dictionary<String, List<String>> {
    {"food", food },
    {"languages", languages },
    {"tech", tech },
    {"colors", colors },
};

Console.WriteLine("Please choose a category: food, languages, tech, or colors.");
string input = Console.ReadLine();
if (lists.ContainsKey(input))
{
    foreach (string item in lists[input])
    {
        Console.ReadLine();
        Console.WriteLine(item);
    }
}

答案 1 :(得分:1)

您可以在程序中使用以下代码-

List<string> food = new List<string> { "banana", "apple", "steak", "chicken" };
List<string> languages = new List<string> { "english", "greek", "spanish", "russian" };
List<string> tech = new List<string> { "facebook", "apple", "google", "samsung" };
List<string> colors = new List<string> { "blue", "green", "yellow", "red" };

while (true)
{
    Console.WriteLine("Please choose a category: food, languages, tech, or colors.");
    string input = Console.ReadLine();
    if (input == "food")
        ProcessList(food);
    else if (input == "languages")
        ProcessList(languages);
    else if (input == "tech")
        ProcessList(tech);
    else if (input == "colors")
        ProcessList(colors);
    else
        break;
}

函数-

    private void ProcessList(List<string> list)
    {
        foreach (string item in list)
        {
            Console.ReadLine();
            Console.WriteLine(item);
        }
    }

我已删除goto,因为不建议这样做。您可以使用while(true)并提供一条break语句来退出程序,以实现类似的功能。在这种情况下,如果用户输入除建议选项以外的任何内容,程序将退出。您可以根据需要修改if else语句。

我也将List更改为通用List<string>

答案 2 :(得分:0)

您可以使用字典,其中“值”是现有的字符串列表,“键”是列表名称。

您可以做类似的事情

var myDictionary = new Dictionary<string, List<string>>();

然后使用您的数据填充。然后:

var list = myDictionary[input];
foreach (string s in list)
{
 Console.ReadLine();
 Console.WriteLine(s);
}

答案 3 :(得分:0)

这是我想出的,效果很好,并且使用了良好的做法!谢谢Gaurav Mathur和D Stanley!

 List<string> food = new List<string> { "banana", "apple", "steak", "chicken" };
        List<string> languages = new List<string> { "english", "greek", "spanish", "russian" };
        List<string> tech = new List<string> { "facebook", "apple", "google", "samsung" };
        List<string> colors = new List<string> { "blue", "green", "yellow", "red" };

        var lists = new Dictionary<String, List<String>> {
            {"food", food },
            {"languages", languages },
            {"tech", tech },
            {"colors", colors },
        };

        while (true)
        {
            Console.WriteLine("Please choose a category: food, languages, tech, or colors.");
            string input = Console.ReadLine();
            if (lists.ContainsKey(input))
            {
                foreach (string item in lists[input])
                {
                    Console.ReadLine();
                    Console.WriteLine(item);
                }
            }
            else
            {
                break;
            }
        }