电话簿示例

发布于 2024-10-12 01:12:45 字数 2355 浏览 10 评论 0原文

我正在尝试在 Windows 控制台应用程序中制作一个简单的电话目录程序。我使用了 SortedDictionary,其中 Key 是名称,Value 是数字,List 表示地址,StringDictionary 表示电子邮件 ID。我将所有这些放在一个名为 Directory 的类中,然后通过 Main 中的实例调用它。我在枚举目录时遇到问题。我想在同一行中打印一个人的所有四个条目。谁能告诉我应该如何进行。这就是我尝试的方式。我很确定我的逻辑中有很多错误。很抱歉给您带来不便:-

public class Directry    
{      
    List <string> Email_id= new List <string>();

    SortedDictionary<string, int> Dict = new SortedDictionary<string, int>();
    StringCollection Adress=new StringCollection();
    public Directry(SortedDictionary<string, int> dict, StringCollection adress, List<string> email)
    {
       this.Dict = dict;
       this.Email_id = email;
       this.Adress = adress;
    }      
}

class Another
{
    static void Main(string[] args)
    {
        SortedDictionary<string, int> dict = new SortedDictionary<string, int>();
        List<string> email = new List<string>();
        StringCollection adres = new StringCollection();
        Directry dir = new Directry( dict, adres,email);
        string key, adress, Email;
        int numbr;
        start_again:
        for (int i = 0; i <2; i++)
        {
            Console.WriteLine("enter name to be added in the directory");
            key = Console.ReadLine();
            Console.WriteLine("enter number to be added in the directory");
            numbr = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter address to be added in the directory");
            adress = Console.ReadLine();
            Console.WriteLine("enter your email-id to be added in the directory");
            Email = Console.ReadLine();
            dict.Add(key, numbr);
            email.Add(Email);
            adres.Add(adress);
        }
        Console.WriteLine("do you wish to continue-y/n?");
        char c = Convert.ToChar(Console.ReadLine());
        if (c == 'y')
        {
            Console.WriteLine("you said yes");
            goto start_again;
        }
        else
        {
            Console.WriteLine("no more entries can be added");
            Console.WriteLine("Name         Number          adress            email");
            foreach (object ot in dir)
            {
                Console.WriteLine(ot);
            }               
        }
        Console.ReadLine();
    }
}

谢谢。

I am trying to make a simple phone directory program in windows console application. I have used SortedDictionary in which Key is name and Value is number, List for address and StringDictionary for email-ids. I put all of these in a class named Directory and then called it through an instance in Main. I am facing problem in enumerating the directory. I want to print all four entries of a person in same line. Can anyone tell me how should I proceed. This is how I was trying.I am well sure of that there are many mistakes in my logic..Sorry for inconvenience:-

public class Directry    
{      
    List <string> Email_id= new List <string>();

    SortedDictionary<string, int> Dict = new SortedDictionary<string, int>();
    StringCollection Adress=new StringCollection();
    public Directry(SortedDictionary<string, int> dict, StringCollection adress, List<string> email)
    {
       this.Dict = dict;
       this.Email_id = email;
       this.Adress = adress;
    }      
}

class Another
{
    static void Main(string[] args)
    {
        SortedDictionary<string, int> dict = new SortedDictionary<string, int>();
        List<string> email = new List<string>();
        StringCollection adres = new StringCollection();
        Directry dir = new Directry( dict, adres,email);
        string key, adress, Email;
        int numbr;
        start_again:
        for (int i = 0; i <2; i++)
        {
            Console.WriteLine("enter name to be added in the directory");
            key = Console.ReadLine();
            Console.WriteLine("enter number to be added in the directory");
            numbr = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter address to be added in the directory");
            adress = Console.ReadLine();
            Console.WriteLine("enter your email-id to be added in the directory");
            Email = Console.ReadLine();
            dict.Add(key, numbr);
            email.Add(Email);
            adres.Add(adress);
        }
        Console.WriteLine("do you wish to continue-y/n?");
        char c = Convert.ToChar(Console.ReadLine());
        if (c == 'y')
        {
            Console.WriteLine("you said yes");
            goto start_again;
        }
        else
        {
            Console.WriteLine("no more entries can be added");
            Console.WriteLine("Name         Number          adress            email");
            foreach (object ot in dir)
            {
                Console.WriteLine(ot);
            }               
        }
        Console.ReadLine();
    }
}

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鱼窥荷 2024-10-19 01:12:45

这段代码远非完美,但它应该可以帮助您上路。

 public class Directory
{
    public List<string> EmailAddresses = new List<string>();
    public List<string> Addresses = new List<string>();

    public void Add(string email, string address)
    {
        EmailAddresses.Add(email);
        Addresses.Add(address);
    }
}

class Another
{
    static void Main(string[] args)
    {

        SortedDictionary<string, Directory> _directory = new SortedDictionary<string, Directory>();
        string key, adress, Email;
        int numbr;
    start_again:
        for (int i = 0; i < 2; i++)
        {
            Console.WriteLine("enter name to be added in the directory");
            key = Console.ReadLine();
            Console.WriteLine("enter number to be added in the directory");
            numbr = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter address to be added in the directory");
            adress = Console.ReadLine();
            Console.WriteLine("enter your email-id to be added in the directory");
            Email = Console.ReadLine();

            Directory dir = new Directory();
            dir.Add(Email, adress);

            _directory.Add(key, dir);

        }
        Console.WriteLine("do you wish to continue-y/n?");
        char c = Convert.ToChar(Console.ReadLine());
        if (c == 'y')
        {
            Console.WriteLine("you said yes");
            goto start_again;
        }
        else
        {
            Console.WriteLine("no more entries can be added");
            Console.WriteLine("Name         Number          adress            email");
            foreach (KeyValuePair<string, Directory> d in _directory)
            {
                Console.WriteLine(string.Format("{0}, {1}, {2}", d.Key, d.Value.Addresses.First(), d.Value.EmailAddresses.First()));
            }
        }
        Console.ReadLine();
    }

This code is far from perfect but it should get you on your way.

 public class Directory
{
    public List<string> EmailAddresses = new List<string>();
    public List<string> Addresses = new List<string>();

    public void Add(string email, string address)
    {
        EmailAddresses.Add(email);
        Addresses.Add(address);
    }
}

class Another
{
    static void Main(string[] args)
    {

        SortedDictionary<string, Directory> _directory = new SortedDictionary<string, Directory>();
        string key, adress, Email;
        int numbr;
    start_again:
        for (int i = 0; i < 2; i++)
        {
            Console.WriteLine("enter name to be added in the directory");
            key = Console.ReadLine();
            Console.WriteLine("enter number to be added in the directory");
            numbr = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter address to be added in the directory");
            adress = Console.ReadLine();
            Console.WriteLine("enter your email-id to be added in the directory");
            Email = Console.ReadLine();

            Directory dir = new Directory();
            dir.Add(Email, adress);

            _directory.Add(key, dir);

        }
        Console.WriteLine("do you wish to continue-y/n?");
        char c = Convert.ToChar(Console.ReadLine());
        if (c == 'y')
        {
            Console.WriteLine("you said yes");
            goto start_again;
        }
        else
        {
            Console.WriteLine("no more entries can be added");
            Console.WriteLine("Name         Number          adress            email");
            foreach (KeyValuePair<string, Directory> d in _directory)
            {
                Console.WriteLine(string.Format("{0}, {1}, {2}", d.Key, d.Value.Addresses.First(), d.Value.EmailAddresses.First()));
            }
        }
        Console.ReadLine();
    }
舟遥客 2024-10-19 01:12:45

您没有以特别好的面向对象方式进行编码。

我建议将您的代码结构更改为以下内容:

public class DirectoryEntry
{
    public string Name { get; set; }
    public string PhoneNumber { get; set; }
    public string Addresss { get; set; }
    public string Email { get; set; }
}

public class Directory
{
    List<DirectoryEntry> _entries;

    public Directory()
    {
        _entries = new List<DirectoryEntry>();
    }

    public List<DirectoryEntry> Entries { get { return _entries; } }
}

您可以充实所有内容以保持目录有序,不允许重复名称或任何您想要的内容。

现在你可以拥有类似的东西

Directory directory = new Directory();
directory.Entries.Add(new DirectoryEntry { Name = "Tom", Number = "01293485943" })

foreach (var entry in directory.Entries.OrderBy(de => de.Name))
{
     Console.WriteLine("Name: {0}, Number: {1}", entry.Name, entry.Number);
}

You aren't coding this in a particularly good OO way.

I would suggest changing your code structure to something along the lines of...

public class DirectoryEntry
{
    public string Name { get; set; }
    public string PhoneNumber { get; set; }
    public string Addresss { get; set; }
    public string Email { get; set; }
}

public class Directory
{
    List<DirectoryEntry> _entries;

    public Directory()
    {
        _entries = new List<DirectoryEntry>();
    }

    public List<DirectoryEntry> Entries { get { return _entries; } }
}

You can flesh all that out to keep the directory ordered, not allow duplicate names, or whatever you want.

Now you can have something like

Directory directory = new Directory();
directory.Entries.Add(new DirectoryEntry { Name = "Tom", Number = "01293485943" })

foreach (var entry in directory.Entries.OrderBy(de => de.Name))
{
     Console.WriteLine("Name: {0}, Number: {1}", entry.Name, entry.Number);
}
自此以后,行同陌路 2024-10-19 01:12:45

从表面上看,您正在声明字典

Directry dir = new Directry( dict, adres,email);

,但是您并没有使用从控制台读取的值来更新它。您可以在 Dictionary 类中创建一个方法,将 dict、adres 和 email 对象传递给它。

编辑以从字典中获取值:

您的 Directory 对象有一个 SortedDictonary,将其公开。

public SortedDictionary<string, int> Dict = new SortedDictionary<string, int>();

然后像这样枚举:

 foreach (KeyValuePair<string, int> kvp in dir.Dict)
            {
                Console.WriteLine(kvp.Key, kvp.Value);
            }

By the looks of it you are declaring the dictionary

Directry dir = new Directry( dict, adres,email);

But then you aren't updating it with the values you read from the console. You could make a method within the Dictionary class which you pass the dict, adres and email objects to it.

Edit to grab values from dictionary:

Your Directry object has a SortedDictonary, make it public.

public SortedDictionary<string, int> Dict = new SortedDictionary<string, int>();

Then enumerate like this:

 foreach (KeyValuePair<string, int> kvp in dir.Dict)
            {
                Console.WriteLine(kvp.Key, kvp.Value);
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文