2011-01-11 4 views
0

나는 Windows 콘솔 응용 프로그램에서 간단한 전화 번호부 프로그램을 만들려고 노력하고 있습니다. SortedDictionary는 Key가 name이고 Value가 숫자이고, List가 주소이고 StringDictionary가 email-ids입니다. 이 클래스들을 모두 Directory라는 클래스에 넣은 다음 Main의 인스턴스를 통해 호출했습니다. 디렉토리를 열거하는 데 문제가 있습니다. 같은 줄에 사람의 네 항목을 모두 인쇄하고 싶습니다. 아무도 내가 어떻게해야하는지 말해 줄 수 없다. 이것은 내가 해 봐야 내 logic..Sorry에서 많은 실수가 불편 있다는 것을 잘 확신했다 방법입니다 -전화 번호부 예

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

감사합니다. 그것의 모양으로

+0

같은이 코드를 컴파일 하는가 수 있을까요? – nik0lias

+0

그것은 foreach 성명에서 나에게 문제를 준다! 나머지는 괜찮아요 .. !! – Batrickparry

답변

2

이 코드는 지금까지 완벽한에서 그러나 그것은 당신의 방법에 당신을 얻을해야합니다

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

는 다음과 같이 열거.

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

일종의 작품 .... 고마워요 .. :) – Batrickparry

+0

나는 모든 것을 올릴 수 있었지만 그걸 어떻게 배울 수 있겠습니까? 추가 도움이 필요하면 그냥 물어보십시오. 대답을 수락하는 것을 잊지 마세요 :) – nik0lias

+0

나는 그것을 조금 편집했습니다 .... 지금은 완벽한 .. !!! 감사!! – Batrickparry

0

당신은 사전을

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

를 선언하지만 당신은 콘솔에서 읽은 값을 업데이트하지 않습니다. 사전 클래스에서 dict, adres 및 email 객체를 전달하는 메서드를 만들 수 있습니다.

편집 사전에서 값을 잡아합니다 :

귀하의 Directry 객체가 SortedDictonary 가지고, 그것을 공개.

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

그래서 ... 제발 내가 뭘해야하는지에 관해서 나를 인도 해주세요 !! 모든 일이 잘못되어 어떻게해야합니까? – Batrickparry

+0

좋아 ... 그게 내 Dicrectory를 업데이 트거야 ... 어떻게 열거합니까? – Batrickparry

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); 
} 
+0

깔끔한 코드 +1, LINQ를 사용해야합니다 ... – nik0lias

관련 문제