首页 > 解决方案 > 如何通过输入流通过字典在c#中创建电话簿?

问题描述

我是编程新手......请有人帮助编写这个程序。

这是我到目前为止写的代码....我知道有很多错误请帮忙。

using System;
using System.Collections.Generic;
using System.IO;
class Solution
{ 
static void Main(String[] args)
{
    int n = Convert.ToInt32(Console.ReadLine());
    for (int i = 0; i < n; i++)
    {
        Dictionary<string, int> phonebook = new Dictionary<string, int>();
        foreach (string name in phonenumber)
        {
            phonebook.Add("name", num);
        }
    }

}

}

标签: c#dictionary

解决方案


using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("How many entries?");
            int entries;
            if(!int.TryParse(Console.ReadLine(), out entries))
            {
                Console.WriteLine("You must enter a valid number");
                Environment.Exit(0);
            }
            Dictionary<string, string> phonebook = new Dictionary<string, string>();
            for(int i = 0; i < entries; i++)
            {
                Console.Clear();
                Console.Write("Enter name:");
                string name = Console.ReadLine();
                Console.Write("Enter phone number:");
                string number = Console.ReadLine();
                phonebook.Add(name, number);
            }
            // do something with your dictionary object
        }
    }
}

推荐阅读