如何在C#中的字典中进行用户输入?

我想制作一本接受用户输入和空格的字典。字典接受输入字符串和整数。


我尝试通过数组提供输入,但不知道如何在两个数组中同时使用空格进行用户输入。


Dictionary<string, int> Directory = new Dictionary<string, int>();

int n = int.Parse(Console.ReadLine());

string[] name = new string[n];

int[] phone_no = new int[n];

for (int i = 0; i < n; i++)

}

    name[i] = Console.ReadLine();

    phone_no[i] = int.Parse(Console.ReadLine());

}

for (int i = 0; i < n; i++)

{

    Directory.Add(name[i], phone_no[i]);

}

我需要帮助进行用户输入,例如:


1.山姆 12345678


2.哈里25468789


牛魔王的故事
浏览 112回答 3
3回答

慕斯709654

请注意,电话号码不是整数,而是字符串。它可能以零开头,如果您将其解析为 int,则会丢失前导零(0123456789 变为 123456789)。另外,我认为“+31 (0)6-12345678”是一个有效的电话号码。这是一个可以完成您想要的操作的示例。它会不断请求输入,直到用户输入“exit”并用电话号码更新姓名。public static void Main(){&nbsp; &nbsp; var directory = new Dictionary<string, string>();&nbsp; &nbsp; // Keep requesting inputs&nbsp; &nbsp; while (true)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string input = Console.ReadLine();&nbsp; &nbsp; &nbsp; &nbsp; // provide a possibility to break the loop.&nbsp; &nbsp; &nbsp; &nbsp; if (input == "exit")&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; string[] items = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);&nbsp; &nbsp; &nbsp; &nbsp; if (items.Length != 2)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Expecting '{Name} {Phonenumber}'");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; directory[items[0]] = items[1];&nbsp; &nbsp; }&nbsp; &nbsp; // TODO: Do something with directory}

精慕HU

您可以使用 String.Split() 分割行,即var pair = Console.ReadLine().Split(' ');Dictionary.Add(pair[0], int.Parse(pair[1]))

蓝山帝景

static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Dictionary<string, int> Directory = new Dictionary<string, int>();&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Enter the Number of inputs");&nbsp; &nbsp; &nbsp; &nbsp; int count = int.Parse(Console.ReadLine());&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < count; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Enter the Name " + i + 1 + " : ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string Name = Console.ReadLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Enter the Age " + i + 1 + " : ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int Age = Convert.ToInt32(Console.ReadLine());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Directory.Add(Name, Age);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Press key to display the contents of your dictionary..");&nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; &nbsp; &nbsp; foreach (var item in Directory)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Name : " + item.Key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Age : " + item.Value);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; }工作小提琴
打开App,查看更多内容
随时随地看视频慕课网APP