Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何迭代KeyValue列表,以及如何从控制台读取键并在C中的控制台上显示其值#_C# - Fatal编程技术网

C# 如何迭代KeyValue列表,以及如何从控制台读取键并在C中的控制台上显示其值#

C# 如何迭代KeyValue列表,以及如何从控制台读取键并在C中的控制台上显示其值#,c#,C#,我将一些城市名称存储为密钥,将其zipcode存储为KeyValuePair列表中的值,并尝试将密钥作为用户输入从控制台读取,并希望在控制台上显示其值。有人能帮我做到这一点吗 List<KeyValuePair<string, string>> City = new List<KeyValuePair<string, string>> (){ new KeyValuePair<string, string>("Ind", "1"

我将一些城市名称存储为密钥,将其zipcode存储为KeyValuePair列表中的值,并尝试将密钥作为用户输入从控制台读取,并希望在控制台上显示其值。有人能帮我做到这一点吗

List<KeyValuePair<string, string>> City = new List<KeyValuePair<string, string>> (){
    new KeyValuePair<string, string>("Ind", "1"),
    new KeyValuePair<string, string>("Aus", "2"),
    new KeyValuePair<string, string>("Pak","3"),
    new KeyValuePair<string, string>("Eng","4"),
    new KeyValuePair<string, string>("USA","5")

};            
foreach (KeyValuePair<string, string> kvp in City)
{               
    Console.WriteLine("Enter City name or Zipcode");
    var input = Console.ReadLine();

    if (input == kvp.Key)
    {
        Console.WriteLine("Zipcode of {0} is:{1}", 
        input, kvp.Value);

        Console.ReadLine();
    }
    else
    {
        Console.WriteLine("Zipcode of {0} is: {1}", 
        input, kvp.Value);
        Console.ReadLine();
    }
}
List City=新列表(){
新的KeyValuePair(“Ind”,“1”),
新的KeyValuePair(“Aus”、“2”),
新的KeyValuePair(“Pak”,“3”),
新的KeyValuePair(“英文”、“4”),
新的KeyValuePair(“美国”、“5”)
};            
foreach(城市中的KeyValuePair kvp)
{               
Console.WriteLine(“输入城市名称或Zipcode”);
var input=Console.ReadLine();
如果(输入==kvp.Key)
{
WriteLine({0}的Zipcode为:{1}),
输入,kvp.值);
Console.ReadLine();
}
其他的
{
WriteLine({0}的Zipcode为:{1}),
输入,kvp.值);
Console.ReadLine();
}
}

使用字典代替列表。此外,while循环是一个无限循环,而不是在列表中的项目上循环。 尝试:

Dictionary City=newdictionary();
城市。添加(“Ind”,“1”);
城市。添加(“澳大利亚”,“2”);
城市。添加(“巴基斯坦”,“3”);
城市。添加(“工程”、“4”);
添加(“美国”、“5”);
while(true)
{
Console.WriteLine(“输入城市名称,按x退出”);
var input=Console.ReadLine();
如果(输入=“x”)
返回;
if(!City.TryGetValue(输入,输出字符串zipCode))
Console.WriteLine(“无效城市”);
WriteLine({0}的Zipcode为:{1}),输入,Zipcode;
}

我不明白您的代码在做什么。如果两个分支都有完全相同的代码,为什么还要使用If语句呢?如果您只是要忽略该输入并显示现有的存储值,为什么还要请求用户输入?为什么不使用
字典而不是
列表
?谢谢您的回答。实际我还在学习,我想在列表中存储一些数据(城市和邮政编码),当我输入城市(键)作为用户输入时,它应该显示该城市的邮政编码(值)。(请忽略下面的代码块)您的消息是:“输入城市名称或Zipcode”,而不是“输入城市名称”。消息是:“输入城市名称或Zipcode”,而不是“输入城市名称”,谢谢@Cid。编辑了消息字符串。根据OP,要求是:
尝试从控制台读取键作为用户输入,并希望在控制台上显示其值
如何实现vise versa,我的意思是,如果我给出值,那么它也应该显示其键,但这两个逻辑应该存在于同一个循环中。而且,如果我输入L或类似的内容,它应该显示字典中存储的所有键(城市),我该怎么做?@sughoshbhat,我鼓励你自己尝试一下。您可以在代码中看到在字典中查找值的if条件。尝试搜索并找到如何从给定C#值的字典中获取密钥。此外,要列出城市,请搜索如何循环浏览字典keyvaluepairs。
     Dictionary<string, string> City = new Dictionary<string, string>();
     City.Add("Ind", "1");
     City.Add("Aus", "2");
     City.Add("Pak", "3");
     City.Add("Eng", "4");
     City.Add("USA", "5");


     while (true)
     {
        Console.WriteLine("Enter City name. Press x to Exit");
        var input = Console.ReadLine();
        if (input == "x")
           return;
        if (!City.TryGetValue(input, out string zipCode))
           Console.WriteLine("Invalid City");

        Console.WriteLine("Zipcode of {0} is:{1}", input, zipCode);
     }