0

Windows コンソール アプリケーションで簡単な電話帳プログラムを作成しようとしています。キーが名前で値が数値であるSortedDictionary、アドレスにリスト、電子メールIDにStringDictionaryを使用しました。これらすべてを Directory という名前のクラスに配置し、Main のインスタンスを介して呼び出しました。ディレクトリを列挙する際に問題に直面しています。ある人物の 4 つのエントリすべてを同じ行に印刷したいと考えています。誰がどうすればいいのか教えてもらえますか。これが私が試みていた方法です.私の論理には多くの間違いがあると確信しています..ご不便をおかけして申し訳ありません:-

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();
    }
}

ありがとう。

4

3 に答える 3

2

これを特に優れたオブジェクト指向プログラミングでコーディングしているわけではありません。

コード構造を次のようなものに変更することをお勧めします...

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);
}
于 2011-01-11T12:52:31.247 に答える
2

このコードは完璧とは言えませんが、あなたのやり方を理解できるはずです。

 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();
    }
于 2011-01-11T12:35:02.677 に答える
0

見た目では、辞書を宣言しています

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

しかし、コンソールから読み取った値で更新していません。Dictionary クラス内に、dict、adres、および email オブジェクトを渡すメソッドを作成できます。

辞書から値を取得するように編集します。

あなたのDirectryオブジェクトにはSortedDictonaryがあり、それを公開してください。

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

次に、次のように列挙します。

 foreach (KeyValuePair<string, int> kvp in dir.Dict)
            {
                Console.WriteLine(kvp.Key, kvp.Value);
            }
于 2011-01-11T11:43:28.893 に答える